Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. bool f=false;
  10. int res=INT_MIN;
  11. string s;
  12. int curLen=0,maxLen=-1,maxInd=-1;
  13. int i,ind,k;
  14. char c;
  15. cin>>k;
  16. cin>>s;
  17. for (i=1;i<s.size();i++)
  18. {
  19. if (!f && s[i]==s[i-1])
  20. {
  21. f=true;
  22. ind=i;
  23. curLen=1;
  24. continue;
  25. }
  26. if (f && s[i]!=s[i-1])
  27. {
  28. curLen++;
  29. continue;
  30. }
  31. if (f && s[i]==s[i-1])
  32. {
  33. if (curLen>maxLen)
  34. {
  35. maxLen=curLen;
  36. maxInd=ind;
  37. }
  38. curLen=1;
  39. ind=i;
  40. continue;
  41. }
  42. }
  43. if (f)
  44. if (curLen>maxLen)
  45. {
  46. maxLen=curLen;
  47. maxInd=ind;
  48. }
  49.  
  50. if (maxInd!=-1)
  51. for (i=maxInd;i<maxInd+maxLen;i++)
  52. {
  53. if (s[i]=='0')
  54. s[i]='1';
  55. else
  56. s[i]='0';
  57. }
  58. c='1';
  59. curLen=0;
  60. for (i=0;i<s.size();i++)
  61. {
  62. if (s[i]!=c)
  63. {
  64. curLen++;
  65. c=s[i];
  66. }
  67. }
  68. res=max(res,curLen);
  69. c='0';
  70. curLen=0;
  71. for (i=0;i<s.size();i++)
  72. {
  73. if (s[i]!=c)
  74. {
  75. curLen++;
  76. c=s[i];
  77. }
  78. }
  79. res=max(res,curLen);
  80. cout<<res;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement