QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#853731 | #8701. Border | ChiFAN | 0 | 11ms | 27376kb | C++20 | 4.4kb | 2025-01-11 18:41:45 | 2025-01-11 18:41:47 |
Judging History
answer
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
const int mod = 1e9+7;
const int inv = (mod+1)/2;
const int Mod = 1e9+9;
const int base = 1331;
const int maxn = 2e6+4;
int _pow[maxn];
mt19937 rd(time(0));
int to[26];
int ch[26];
inline int qpow(int a,int b){
int res=1;
for(int i=0;i<32;i++){
if((1ll<<i)&b) res=1ll*res*a%mod;
a=1ll*a*a%mod;
}
return res;
}
int S[maxn],T[maxn],P[maxn],v1[maxn],v2[maxn],v3[maxn],v4[maxn];
bool B=true;
vector<int> solve(string s,string t){
vector<int> ans;
int n=s.size();
for(int i=1;i<=n;i++) S[i]=to[s[i-1]-'a'],T[i]=to[t[i-1]-'a'],P[i]=ch[s[i-1]-'a'];
ans.resize(n+1);
for(int i=1;i<=n;i++){
v1[i]=(1ll*v1[i-1]+S[i])%mod;
v2[i]=(1ll*v2[i-1]+1ll*S[i]*S[i]%mod)%mod;
v3[i]=(1ll*v3[i-1]+1ll*S[i]*i%mod)%mod;
v4[i]=(1ll*v4[i-1]+1ll*P[i]*_pow[i]%Mod)%Mod;
}
array<int,maxn> suf;
for(int i=1;i<n;i++){
if(i*2>n&&B==false) break;
int pos=0;
bool flag=true;
for(int j=2;i*(j-1)+1<=n;j++){
int L=(j-1)*i+1,R=j*i;
int l=1,r=i;
if(R>n){
int c=R-n;
R-=c,r-=c;
}
if((1ll*v1[r]+mod-v1[l-1])%mod==(1ll*v1[R]+mod-v1[L-1])%mod&&(1ll*v3[r]+mod-v3[l-1])%mod==((1ll*v3[R]+mod-v3[L-1])%mod+mod-1ll*(L-l)*((1ll*v1[R]+mod-v1[L-1])%mod)%mod)%mod) continue;
else{
int c1=(1ll*v2[r]+v2[L-1]+2*mod-v2[l-1]-v2[R])%mod,c2=(1ll*v1[r]+v1[L-1]+2*mod-v1[l-1]-v1[R])%mod;
c1=1ll*c1*qpow(c2,mod-2)%mod;
int x=(1ll*c1+c2)%mod*inv%mod;
int y=(1ll*c1+mod-c2)%mod*inv%mod;
int c3=(1ll*v3[R]+mod-v3[L-1])%mod;
c3=(1ll*c3+mod-1ll*(L-l)*((1ll*v1[R]+mod-v1[L-1])%mod)%mod)%mod;
int p=1ll*(((1ll*v3[r]+mod-v3[l-1])%mod+mod-c3)%mod)*qpow((1ll*x+mod-y)%mod,mod-2)%mod;
if(p<1||p>n){
flag=false;
break;
}
if(S[p]!=x){
flag=false;
break;
}
if(p+(L-l)>n){
flag=false;
break;
}
if(S[p+(L-l)]!=y){
flag=false;
break;
}
if(T[p+(L-l)]!=x){
flag=false;
break;
}
//[l,p-1] [L,p-1+L-l]
if((1ll*v4[p-1]+Mod-v4[l-1])*_pow[L-l]%Mod!=(1ll*v4[p-1+L-l]+Mod-v4[L-1])%Mod){
flag=false;
break;
}
//[p+1,r] [p+1+L-l,R]
if((1ll*v4[r]+Mod-v4[p])%Mod*_pow[L-l]%Mod!=(1ll*v4[R]+Mod-v4[p+L-l])%Mod){
flag=false;
break;
}
if(pos==0) pos=p+(L-l);
else{
flag=false;
break;
}
}
}
if(flag==false) continue;
if(pos!=0){
ans[pos]=max(ans[pos],n-i);
}else if(i*2>=n){
suf[i]=max(suf[i],n-i);
}
}
for(int i=n-1;i>=1;i--) suf[i]=max(suf[i],suf[i+1]);
for(int i=1;i<=n;i++) ans[i]=max(ans[i],suf[max(i,n-i+1)]);
return ans;
}
bool check(){
for(int i=0;i<26;i++){
for(int j=0;j<i;j++){
if(ch[i]==ch[j]||to[i]==to[j]) return false;
}
}
return true;
}
int main(){
while(check()==false) for(int i=0;i<26;i++) to[i]=rd()%mod,ch[i]=rd()%Mod;
_pow[0]=1;
for(int i=1;i<maxn;i++) _pow[i]=1ll*_pow[i-1]*base%Mod;
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
string s,t;
cin>>s>>t;
vector<int> ans=solve(s,t);
reverse(s.begin(),s.end());
reverse(t.begin(),t.end());
B=false;
vector<int> oth=solve(s,t);
reverse(oth.begin(),oth.end());
vector<int> nxt;
nxt.resize(s.size());
reverse(s.begin(),s.end());
reverse(t.begin(),t.end());
nxt[0]=-1;
for(int i=1;i<s.size();i++){
int z=nxt[i-1];
while(s[z+1]!=s[i]&&z>-1) z=nxt[z];
if(s[z+1]==s[i]) z++;
nxt[i]=z;
}
for(int i=0;i<s.size();i++){
if(s[i]==t[i]) cout<<nxt[s.size()-1]+1<<'\n';
else cout<<max(ans[i+1],oth[i])<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 23
Accepted
time: 11ms
memory: 27376kb
input:
cbaababaabacbaababaabacbaabacbaababaabacbaaba dabbababbabaabbafabbgbaabfebaabzababbayaabcac
output:
0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 17 17 17 17 17 17 17 17 17 17 17 6 6 6 6 6 6 6 6 6 6 6 0 0 0 3 0 1
result:
ok 45 numbers
Test #2:
score: 0
Wrong Answer
time: 7ms
memory: 26900kb
input:
cbaababaabacbaabadbaababaabacbaabacbaaba aabwaxjbbabtalbabcasbabibbabaabbabaabiac
output:
0 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6 6 23 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 1
result:
wrong answer 1st numbers differ - expected: '3', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%