QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#339159 | #5312. Levenshtein Distance | xlwang | WA | 560ms | 33116kb | C++14 | 4.5kb | 2024-02-26 20:21:42 | 2024-02-26 20:21:44 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define randfind(l,r) (rnd()%((r)-(l)+1)+(l))
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
//inline void init(){
// int t=read();
// while(t--) work();
//}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int Maxn=2e5+10;
char s[Maxn],t[Maxn],c[Maxn];
int k;
int n,m;
int ln;
namespace LCP{
int h[Maxn];
int rk[Maxn],sa[Maxn],num[Maxn];
inline int getid(char c){
if(c>='a' && c<='z') return c-'a'+1;
else return 30;
}
int old[Maxn];
int p[Maxn];
inline void out(){
puts("out:");
for(int i=1;i<=ln;++i) cout<<i<<' '<<sa[i]<<' '<<rk[i]<<endl;
}
int lg[Maxn];
int f[Maxn][20];
inline int getln(int l,int r){
if(l>r) return 0;
int ln=lg[r-l+1];
return min(f[l][ln],f[r-(1<<ln)+1][ln]);
}
inline int getans(int l,int r){
l=rk[l],r=rk[r+n+1];
if(l>r) swap(l,r);
return getln(l+1,r);
}
inline void into(){
int val=127;
fr(i,1,ln) rk[i]=c[i],num[rk[i]]++;
// fr(i,1,ln) cout<<i<<' '<<rk[i]<<endl;
fr(i,1,val) num[i]+=num[i-1];
rf(i,ln,1) sa[num[rk[i]]--]=i;
fr(i,1,ln) old[i]=rk[i];
int lst=0;
fr(i,1,ln){
if(i==1) rk[sa[i]]=++lst;
else {
if(old[sa[i]]==old[sa[i-1]]) rk[sa[i]]=lst;
else rk[sa[i]]=++lst;
}
}
lst=ln;
// out();
for(int len=1;len<=ln;len*=2){
val=0;
memset(num,0,sizeof(num));
fr(i,1,ln) p[i]=sa[i];
fr(i,1,ln) ++num[rk[p[i]+len]];
fr(i,1,lst) num[i]+=num[i-1];
rf(i,ln,1) sa[num[rk[p[i]+len]]--]=p[i];
memset(num,0,sizeof(num));
fr(i,1,ln) p[i]=sa[i];
fr(i,1,ln) ++num[rk[p[i]]];
fr(i,1,lst) num[i]+=num[i-1];
rf(i,ln,1) sa[num[rk[p[i]]]--]=p[i];
fr(i,1,ln) old[i]=rk[i];
fr(i,1,ln){
if(i==1) rk[sa[i]]=++val;
else {
if(old[sa[i]]==old[sa[i-1]] && old[sa[i]+len]==old[sa[i-1]+len]) rk[sa[i]]=val;
else rk[sa[i]]=++val;
}
}
// writeln(len);out();
if(val==ln) break;lst=val;
}
int now=0;
// fr(i,1,ln) cout<<c[i];cout<<endl;
fr(i,1,ln){
if(now) --now;
// cout<<i<<' '<<sa[rk[i]-1]<<endl;
while(i+now<=ln && sa[rk[i]-1]+now<=ln && c[i+now]==c[sa[rk[i]-1]+now]) ++now;
h[rk[i]]=now;
}
// fr(i,1,ln) cout<<i<<' '<<rk[i]<<' '<<h[rk[i]]<<endl;
lg[0]=-1;
fr(i,1,ln) f[i][0]=h[i];
fr(i,1,ln) lg[i]=lg[i/2]+1;
fr(j,1,lg[ln]) fr(i,1,ln){
if(i+(1<<j)-1>ln) break;
f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
// fr(i,1,n) fr(j,1,m) cout<<i<<' '<<j<<' '<<getans(i,j)<<endl;
}
}
int f[200][200];
inline void init(){
k=read();
scanf("%s",s+1);n=strlen(s+1);
scanf("%s",t+1);m=strlen(t+1);
fr(i,1,n) c[i]=s[i];
c[n+1]='#';
fr(i,1,m) c[n+1+i]=t[i];
ln=n+m+1;
}
int ans[200];
int minn[Maxn];
inline void work(){
LCP::into();
fr(st,0,m-1){
fr(i,0,k-1) fr(j,-i,i) f[i][j+k]=-1;
f[0][0+k]=LCP::getans(1,st+1)+1;
fr(i,0,k-1) fr(j,-i,i){
if(f[i][j+k]==-1) continue;
int x=f[i][j+k];
// cout<<"*"<<i<<' '<<j<<' '<<x<<endl;
if(x<=n) f[i+1][j-1+k]=max(f[i+1][j-1+k],x+1+LCP::getans(x+1,st+x+j));
if(x+j+st<=m) f[i+1][j+1+k]=max(f[i+1][j+1+k],x+LCP::getans(x,st+x+j+1));
if(x<=n && x+j+st<=m) f[i+1][j+k]=max(f[i+1][j+k],x+1+LCP::getans(x+1,st+x+j+1));
}
int L,R;
L=max(st+n-k-k,st+1);R=min(st+n+k+k,m);
// writeln(st);
// fr(i,0,k) fr(j,-i,i) cout<<i<<' '<<j<<' '<<f[i][j+k]<<endl;
// writeln(st);
// cout<<L<<' '<<R<<endl;
fr(i,L,R) minn[i]=k+1;
fr(i,0,k) fr(j,-i,i) if(f[i][j+k]==n+1){
minn[st+j+n]=min(minn[st+j+n],i);
}
fr(i,L+1,R) minn[i]=min(minn[i],minn[i-1]+1);
rf(i,R-1,L) minn[i]=min(minn[i],minn[i+1]+1);
// fr(i,L,R) cout<<i<<' '<<minn[i]<<endl;
fr(i,L,R) if(minn[i]<=k) ans[minn[i]]++;
}
fr(i,0,k) writeln(ans[i]);
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
init();work();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 16140kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: 0
Accepted
time: 560ms
memory: 30788kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 31 numbers
Test #3:
score: -100
Wrong Answer
time: 382ms
memory: 33116kb
input:
30 BBAAABAABBBA ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...
output:
17 662 8230 50302 166666 310121 345469 280387 227200 209178 203013 198561 105117 102147 99771 97730 96058 94730 93633 92720 92060 91525 91143 90831 90585 90419 90288 90200 90120 90068 90188
result:
wrong answer 31st numbers differ - expected: '90035', found: '90188'