项目场景:
Time Limit: | 200 ms |
Memory Limit: | 128 MB |
Case score: | 10 |
Level: | 2 |
Type: | Traditional Problem |
Validator: | Generic Comparsion |
问题描述
Description
\text{Smart}Smart 有一个只包含 0,10,1 的字符串,设这个字符串长度为 lenlen,那么它是个下标为 1 \sim len1∼len 的字符串,它想找到一个位置 x(1≤x \lt len)x(1≤x<len),使得1 \sim x1∼x中的11 的个数与 x+1 \sim lenx+1∼len中的 00 的个数相等,如果存在多个位置,则输出最小的。如果不存在一个合法的 xx,则输出 len+1len+1。
Input
一个字符串 ss。
Output
一个整数表示答案。
Sample Input
01100
Sample Output
3
Hint
40\%40% 的数据:len≤255len≤255;
60\%60% 的数据:len≤1000len≤1000;
100\%100% 的数据:len≤100000len≤100000。
参考代码:
#include<bits/stdc++.h> using namespace std; char s[100009]; int x,count1=0,count0=0,a[100009],b[100009]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>(s+1); int len=strlen(s+1); for(int i=1;i<=len;i++){ if(s[i]=='0') a[i]=a[i-1]; else a[i]=a[i-1]+1; } for(int i=len;i>=1;i--){ if(s[i]=='1') b[i]=b[i+1]; else b[i]=b[i+1]+1; } for(x=1;x<len;x++){ if(a[x]==b[x+1]){ break; } } if(x==len) cout<<len+1; else cout<<x; return 0; }
原因分析:
正向累加a,反向累加b,考虑不全面
解决方案:
看笔记