codeforces #334 div 2 C. Alternative Thinking
题意:给定一个01串。要进行一次变换:选一段连续的非空的字串,将这段串的0和1反转(0变成1,1变成0) 然后问能得到的最长的0,1交替的序列的长度是多少(不一定连续)
比赛的时候想出来两种会将答案增加的可能情况。一种是10000001 中间有大于等于3个的连续字符,这样可以把中间反转一下,答案会+2 另外一种是 1001001 这样。。有至少两段的连续两个以上的相同字符被另一个字符隔开的情况。只要将1001001变成1010101。答案还是会+2。。。然后发现这两种情况实际上可以统一起来。即:有至少两段的连续相同字符。 注意000 也算有两段。 如果有两段或者以上,那么答案+2.
1/* ***********************************************
2Author :111qqz
3Created Time :2015年12月02日 星期三 00时36分47秒
4File Name :code/cf/#334/C.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34char a[N];
35int n;
36
37char tow(char ch)
38{
39 if (ch=='0') return '1';
40 if (ch=='1') return '0';
41}
42int main()
43{
44 #ifndef ONLINE_JUDGE
45 freopen("code/in.txt","r",stdin);
46 #endif
47
48 scanf("%d",&n);
49 scanf("%s",a);
50 int cnt = 0;
51 for ( int i = 0 ; i < n-1 ; i++)
52 if (a[i]==a[i+1])
53 cnt++;
54
55 if (cnt>=2) cnt = 2;
56
57 char tar = a[0];
58 tar = tow(tar);
59 int ans = 1;
60 for ( int i = 1 ; i < n ; i++ )
61 {
62// printf("%d %c\n",i,tar);
63 if (a[i]==tar)
64 {
65 ans++;
66 tar = tow(tar);
67 }
68 }
69
70 printf("%d\n",ans+cnt);
71
72
73 #ifndef ONLINE_JUDGE
74 fclose(stdin);
75 #endif
76 return 0;
77}