51nod 1391 01 String (Exercise thinking good question)

Source: Internet
Author: User


Topic http://www.51nod.com/onlineJudge/questionCode.html#problemId=1391?iceId=20917


1391 01 Strings Source: codility Base time limit: 1 seconds space limit: 131072 KB Score: 40 Difficulty: 4-level algorithm problem

Given a 01 string s, find a substring of it as long as possible s[I.. J], satisfies the existence of a position i<=x <=j, s[i. X] in more than 0:1, and S[x + 1..J] in more than 1:0. The length of the oldest string that satisfies the condition. Input
A row contains a string s that is composed only of 0 and 1. The length of S is not more than 1000000.
Output
A row contains an integer that represents the length of the oldest string that satisfies the requirement.
Input example
10
Output example
0


Problem Solving Ideas:

Very interesting a topic, see the topic easy to let us think, 2 times pre-treatment, 1 times in the past to find out to this position 0 of the number is greater than the maximum of 1

Length, and one time from the trip to find the number of this position 1 is greater than 0. But how to find it? Then we need a little skill, we let 0 generations

Table -1,1 represents 1, then a paragraph and less than 0, then 0 of the number is greater than 1, greater than 0 represents the number of 1 is greater than the number of 0, so it is converted into this position

The maximum length is greater than 0 or less than 0.

so far, we can not solve the problem, we take from the post-processing as an example, when from the starting position to here and less than 0 when, well disposed of, this long

The degree is, but when and greater than equals 0 o'clock what? Assuming that the value here is cur (cur>=0), see if the front appears cur+1, cur+1, the earliest

Now cur+1 position to the current value is to meet the number of 0 is greater than 1 of the number of the longest length, why the cur+2,cur+3 also meet the requirements, but from

it The beginning of some values must be less than the length from cur+1, certain, because cur+2,cur+3 must be in the back of the cur+1, so the topic can be done

The


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace     std;const int maxn=1000000+1000;int h[maxn];int a[maxn];int b[maxn];int c[maxn];char s[maxn];int Main () {scanf ("%s", s);    int N=strlen (s);    int cur=0;        for (int i=0;i<n;i++) {if (s[i]== ' 0 ') a[i]=-1;    else a[i]=1;    } memset (H,-1,sizeof (h));    memset (B,0,sizeof (a));        for (int i=0;i<n;i++) {cur+=a[i];        if (cur<0) b[i]=i+1;            else {if (h[cur+1]!=-1) {b[i]=i-h[cur+1];                } else {h[cur]=i;            b[i]=0;    }}} memset (H,-1,sizeof (h));    Memset (C,0,sizeof (c));    cur=0;        for (int i=n-1;i>=0;i--) {cur+=a[i];        if (cur>0) c[i]=n-i;            else {if (h[-(cur-1)]!=-1) {c[i]=h[-(cur-1)]-i;            }else {c[i]=0;            h[-(cur)]=i;    }}} int ans=0;    for (int i=0;i<n;i++) {if (b[i]>0&&c[i+1]>0) Ans=max (B[i]+c[i+1],ans);    } cout<<ans<<endl; return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

51nod 1391 01 String (Exercise thinking good question)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.