题意:
一个由0和1组成的字符串s,长度小于等于1e5,它可以构成字符串t, t=sssss....,t是无限长的。在t的前缀中(空串也是前缀)找出0的个数和1的个数之差(个数差记为d)为x的前缀,输出个数。x是在-1e9~1e9范围内。如果有无穷个则输出-1。
思路:
首先考虑特殊情况。前缀个数为0的时候,是在s中不存在个数差d等于x的前缀,并且s中0和1的个数相同。
无穷个是s中存在个数差d等于x的前缀,且s中0和1的个数相同。
对于一般的情况,用map记录下所有前缀的个数差d的个数,还有出现的最大值最小值。然后从最小值开始遍历,找出符合的前缀,更新答案。
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
char s[100005];
map<int, int>mp;
int main()
{
int n, x, i, MAX, MIN, d, T;
int cnt0, cnt1, ans;
scanf("%d", &T);
while (T--) {
mp.clear();
MAX = 0; MIN = 0; d = 0;
ans