QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282524 | #7748. Karshilov's Matching Problem II | xiaolang | WA | 116ms | 57176kb | C++14 | 1.8kb | 2023-12-12 11:18:21 | 2023-12-12 11:18:22 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+5;
char s[N],t[N];
long long ex[N],z[N];
int lens,lent;
int nxt[N];
int n,m;
void kmp_nxt(){
nxt[0]=-1;
nxt[1]=0;
int k=0;
for(int i=2;i<=n;i++){
while(s[i]!=s[k+1]&&k>=0)k=nxt[k];
nxt[i]=++k;
}
}
void exkmp_nxt(){
int maxpos=1,maxr=0;
z[1]=lent;
for(int i=2;i<=n;i++){
if(i<=maxr)z[i]=min(maxr-i+1,z[i+1-maxpos]);
else z[i]=0;
while(i+z[i]<=n&&s[z[i]+i]==s[z[i]+1])z[i]++;
if(i+z[i]-1>maxr){
maxpos=i;
maxr=i+z[i]-1;
}
}
}
void exkmp(){
int maxpos=1,maxr=0;
for(int i=1;i<=n;i++){
if(i<=maxr)ex[i]=min(maxr-i+1,z[i+1-maxpos]);
else ex[i]=0;
while(i+ex[i]<=n&&t[ex[i]+i]==s[ex[i]+1])ex[i]++;
if(i+ex[i]-1>maxr){
maxpos=i;
maxr=i+ex[i]-1;
}
}
}
int delta[N];
int c[N];
int ans[N],d[N];
int sc[N],ss[N];
int f[N][22],_log[N];
signed main(){
cin>>n>>m;
cin>>s+1>>t+1;
kmp_nxt();
exkmp_nxt();
exkmp();
for(int i=1;i<=n;i++){
scanf("%lld",&c[i]);
sc[i]=sc[i-1]+c[i];
}
for(int i=1;i<=n;i++){
d[i]=c[i]+d[nxt[i]];
ans[i]=ans[i-1]+d[i];
}
for(int i=1;i<=n;i++){
ss[i]=ss[i-1]+sc[ex[i]];
delta[i]=i+ex[i]-1;
f[i][0]=delta[i];
//cout<<ex[i]<<" ";
}
//cout<<"\n";
_log[1]=0;
for(int i=1;i<=n;i++){
if(i==(1<<(_log[i-1]+1)))_log[i]=_log[i-1]+1;
else _log[i]=_log[i-1];
}
/*for(int i=1;i<=n;i++){
delta[i]=max(delta[i-1],delta[i]);
cout<<delta[i]<<"\n";
}*/
for(int i=1;i<=_log[n];i++){
for(int j=1;j<=n-(1<<i)+1;j++){
f[j][i]=max(f[j][i-1],f[j+(1<<(i-1))][i-1]);
}
}
for(int i=1;i<=m;i++){
int x,y;
scanf("%lld%lld",&x,&y);
int noww=x;
for(int j=_log[y-x+1];j>=0;j--){
if(f[noww][j]<y)noww+=(1<<j);
}
cout<<ans[y-noww+1]+ss[noww-1]-ss[x-1]<<"\n";//l~ans-1
//t[ans~r]=s[1~r-ans+1]
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15828kb
input:
8 5 abbabaab aababbab 1 2 4 8 16 32 64 128 1 1 2 3 3 5 4 7 1 8
output:
1 3 3 16 38
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 3ms
memory: 15828kb
input:
15 4 heheheheehhejie heheheheheheheh 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 2 3 4 8 2 6 1 15
output:
3 13 13 174
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 116ms
memory: 57176kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 -459824331358052 65824434822005 -183328745269971 197732558443069 -189658860121709 -190571395750491 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 440749260586...
result:
wrong answer 8th lines differ - expected: '58850354020997', found: '-459824331358052'