E. Check Transcription
题意
给你一个01串s,一个字符串t,
做法
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
typedef unsigned long long ull;
const int maxn = 1e6+10;
char str[maxn],t[maxn];
template <int T>
struct StringHash
{
std::vector<std::vector<int>> ha, pw;
std::vector<int> M;
explicit StringHash(const std::string& s)
: StringHash(std::vector<int>(s.begin(), s.end())) {}
explicit StringHash(const std::vector<int>& vec)
{
pw = ha =
std::vector<std::vector<int>>(T, std::vector<int>(vec.size() + 1));
std::vector<int> C;
for (int i = 0; i < T; i++)
{
pw[i][0] = 1;
C.push_back(rand_int());
M.push_back(rand_int());
}
for (int z = 0; z < T; z++)
{
for (size_t i = 0; i < vec.size(); ++i)
{
ha[z][i + 1] = (1LL * ha[z][i] * C[z] + vec[i]) % M[z],
pw[z][i + 1] = 1LL * pw[z][i] * C[z] % M[z];
}
}
}
// hash value of interval [a, b)
std::vector<int> hash_interval(int a, int b)
{
std::vector<int> ret(T);
for (int z = 0; z < T; z++)
{
ret[z] = (ha[z][b] - 1LL * ha[z][a] * pw[z][b - a] % M[z] + M[z]) % M[z];
}
return ret;
}
static int rand_int() {
static std::mt19937 gen((std::random_device())());
static std::uniform_int_distribution<int> uid(1e8, 1e9);
return uid(gen);
}
};
int num[2];
int main()
{
scanf("%s%s",str,t);
StringHash<10> sh(t);
int lens=strlen(str);
int lent=strlen(t);
int ans=0;
for(int i=0;i<lens;i++)
{
if(str[i]=='0') num[0]++;
else num[1]++;
}
for(int i=1;i*num[1]<lent;i++)
{
if(((lent-i*num[1])%num[0])!=0) continue;
int len0=(lent-i*num[1])/num[0];
int len1=i;
vector<int> flag0,flag1;
int f0=-1,f1=-1;
int flag=0;
int tmp=0;
for(int j=0;j<lens;j++)
{
if(str[j]=='0')
{
if(f0==-1)
{
f0=1;
flag0=sh.hash_interval(tmp,tmp+len0);
tmp+=len0;
}
else
{
vector<int> tt=sh.hash_interval(tmp,tmp+len0);
if(tt!=flag0)
{
flag=1;
break;
}
else
{
tmp+=len0;
}
}
}
else
{
if(f1==-1)
{
f1=1;
flag1=sh.hash_interval(tmp,tmp+len1);
tmp+=len1;
}
else
{
vector<int> tt=sh.hash_interval(tmp,tmp+len1);
if(tt!=flag1)
{
flag=1;
break;
}
else
{
tmp+=len1;
}
}
}
}
if(flag0!=flag1&&flag==0) ans++;
}
printf("%d\n",ans);
return 0;
}