Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- bool f=false;
- int res=INT_MIN;
- string s;
- int curLen=0,maxLen=-1,maxInd=-1;
- int i,ind,k;
- char c;
- cin>>k;
- cin>>s;
- for (i=1;i<s.size();i++)
- {
- if (!f && s[i]==s[i-1])
- {
- f=true;
- ind=i;
- curLen=1;
- continue;
- }
- if (f && s[i]!=s[i-1])
- {
- curLen++;
- continue;
- }
- if (f && s[i]==s[i-1])
- {
- if (curLen>maxLen)
- {
- maxLen=curLen;
- maxInd=ind;
- }
- curLen=1;
- ind=i;
- continue;
- }
- }
- if (f)
- if (curLen>maxLen)
- {
- maxLen=curLen;
- maxInd=ind;
- }
- if (maxInd!=-1)
- for (i=maxInd;i<maxInd+maxLen;i++)
- {
- if (s[i]=='0')
- s[i]='1';
- else
- s[i]='0';
- }
- c='1';
- curLen=0;
- for (i=0;i<s.size();i++)
- {
- if (s[i]!=c)
- {
- curLen++;
- c=s[i];
- }
- }
- res=max(res,curLen);
- c='0';
- curLen=0;
- for (i=0;i<s.size();i++)
- {
- if (s[i]!=c)
- {
- curLen++;
- c=s[i];
- }
- }
- res=max(res,curLen);
- cout<<res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement