1396 还是01串
程序员文章站
2022-06-02 12:05:37
...
#include<iostream>
#include<string>
using namespace std;
const int MAXN = 1000000 + 10;
int cntOne[MAXN], cntZero[MAXN];
int main(){
ios::sync_with_stdio(false);
string s;
cin >> s;
for(int i = 0; i < s.length(); ++i){
if(i == 0){
if(s[0] == '0') cntZero[0] = 1;
else cntOne[0] = 1;
continue;
}
cntZero[i] = cntZero[i - 1];
cntOne[i] = cntOne[i - 1];
if(s[i] == '0') cntZero[i]++;
else cntOne[i]++;
}
int flag = 0;
for(int i = 0; i <= s.length(); ++i){
if(cntZero[i - 1] == cntOne[s.length() - 1] - cntOne[i - 1]){
flag = 1;
cout << i << endl;
break;
}
}
if(flag == 0) printf("-1\n");
return 0;
}