Problem E: 傅老大练神功
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 43 Solved: 13
Submit
Description
傅老大最近得到的一本葵花宝典,他学会一招踩一脚可以将连续区间树叶子翻转过来,正面变反面,反面变正面(然而并没有什么用),傅老大现在想知道他踩一脚后最多有多少树叶处于正面
注意:傅老大至少踩一片树叶
Input
含有多组测试数据
一组一行01的字符串,1代表树叶处于正面,0代表树叶处于反面
字符串长度最长不超过1000
Output
一行一个数字代表傅老大踩完一脚之后最多有多少树叶处于正面(至少踩一个地方)
Sample Input
10010
010
Sample Output
4
2
HINT
对于第一个样例,傅老大踩区间[2,3],序列变为11110。
对于第二个样例,傅老大踩整个区间,序列变为101。
求最大字串和
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #define ll long long using namespace std; void dis(int a[], int n){ printf("总数为%d个\n",n); for(int i = 1; i <= n; i++) cout<<a[i]<<", "; cout<<endl<<"------------------"<<endl; } const int mx = 1100; char st[mx]; int num[mx],dp[mx]; int main(){ while(scanf("%s",st+1) != EOF){ int len = strlen(st+1); memset(num,0,sizeof(num)); memset(dp,0,sizeof(dp)); //cout<<len<<endl; for(int i = 1; i <= len ;i++){ if(st[i] == '1'){ num[i] = num[i-1]+1; dp[i] = -1; } else{ num[i] = num[i-1]; dp[i] = 1; } } //dis(num,len); int ans = 0,te=-3,nowbe,nowen,nowmx = -1,mxb,mxe; for(int i = 1; i <= len; i++){ if(nowmx < 0){ nowbe = i; nowen = i; nowmx = dp[i]; } else{ nowen = i; nowmx += dp[i]; } if(nowmx >te){ te = nowmx; mxb =nowbe; mxe = nowen; //ans = max(ans,num[nowbe-1]+num[len - nowen]+nowmx); } } //printf("mxb =%d, mxe =%d\n",mxb,mxe); for(int i = mxb; i <= mxe; i++) if(st[i] == '0') ans++; //printf("qian =%d, hou =%d\n",num[mxb-1],num[len - mxe]); ans += num[mxb-1]+num[len] - num[mxe]; cout<<ans<<endl; } return 0; }