QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#353501 | #5312. Levenshtein Distance | Graphcity | WA | 414ms | 32224kb | C++20 | 2.9kb | 2024-03-14 10:11:56 | 2024-03-14 10:11:56 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=1e6;
int n,m,k; char s[Maxn+5],t[Maxn+5];
int rk[Maxn+5],sa[Maxn+5],h[Maxn+5],st[Maxn+5][20];
int ans[Maxn+5],f[32][64],mn[Maxn+5];
struct SA
{
int n,i,t,p,m,id[Maxn+5],num[Maxn+5],old[Maxn+5],cnt[Maxn+5]; char s[Maxn+5];
inline int cmp(int x,int y) {return (old[x]==old[y] && old[x+t]==old[y+t]);}
inline void Run()
{
m=300; For(i,1,m) cnt[i]=0;
for(i=1;i<=n;++i) cnt[rk[i]=s[i]]++;
for(i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for(i=n;i>=1;--i) sa[cnt[rk[i]]--]=i;
for(t=1;t<=n;t<<=1,m=p)
{
for(p=0,i=n;i>n-t;--i) id[++p]=i;
for(i=1;i<=n;++i) if(sa[i]>t) id[++p]=sa[i]-t;
For(i,1,m) cnt[i]=0;
for(i=1;i<=n;++i) cnt[num[i]=rk[id[i]]]++;
for(i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for(i=n;i>=1;--i) sa[cnt[num[i]]--]=id[i];
For(i,1,n) old[i]=rk[i];
for(p=0,i=1;i<=n;++i) rk[sa[i]]=cmp(sa[i],sa[i-1])?p:++p;
if(p==n) break;
}
for(int i=1;i<=n;++i) rk[sa[i]]=i,h[i]=0;
for(int i=1,it=0;i<=n;++i)
{
if(rk[i]==0) continue; if(it) --it;
while(s[i+it]==s[sa[rk[i]-1]+it]) ++it;
h[rk[i]]=it;
}
For(i,1,n) st[i][0]=h[i];
For(j,1,19) for(int i=1;i+(1<<j)-1<=n;++i)
st[i][j]=min(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
} S;
inline int LCP(int x,int y)
{
if(x<=0 || x>n || y<=0 || y>m) return 0;
int l=rk[x],r=rk[y+n+1]; if(l>r) swap(l,r);
int len=__lg(r-l++); return min(st[l][len],st[r-(1<<len)+1][len]);
}
inline int& F(int x,int y) {return f[x][y+k];}
inline void Init()
{
scanf("%d%s%s",&k,s+1,t+1),n=strlen(s+1),m=strlen(t+1);
s[n+1]='#',t[m+1]='$'; S.n=n+m+2;
For(i,1,n+1) S.s[i]=s[i]; For(i,1,m+1) S.s[n+1+i]=t[i]; S.Run();
}
int main()
{
// freopen("1.in","r",stdin);
Init();
For(st,0,m-1)
{
memset(f,-1,sizeof(f)),F(0,0)=0;
For(i,0,k) For(j,-i,i) if(F(i,j)!=-1)
{
F(i,j)+=LCP(F(i,j)+1,st+F(i,j)+j+1);
if(i<k)
{
F(i+1,j+1)=max(F(i+1,j+1),F(i,j));
F(i+1,j)=max(F(i+1,j),F(i,j)+1);
F(i+1,j-1)=max(F(i+1,j-1),F(i,j));
}
}
int L=max(st+1,st+n-k),R=min(m,st+n+k); if(L>R) continue;
For(i,L,R) mn[i]=k+1;
For(i,0,k) For(j,-i,i) if(F(i,j)>=n)
mn[min(m,st+n+j)]=min(mn[min(m,st+n+j)],i);
For(i,L+1,R) mn[i]=min(mn[i],mn[i-1]+1);
Rof(i,R-1,L) mn[i]=min(mn[i],mn[i+1]+1);
For(i,L,R) if(mn[i]<=k) ans[mn[i]]++;
// if(st==0) cerr<<F(2,-2)<<endl;
// For(i,L,R) cerr<<st<<' '<<i<<' '<<mn[i]<<endl;
}
For(i,0,k) printf("%d\n",ans[i]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 20208kb
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: 375ms
memory: 32224kb
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: 414ms
memory: 28448kb
input:
30 BBAAABAABBBA ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...
output:
17 584 6790 39837 131169 258363 323933 302363 263909 230429 211955 206036 126580 104746 100093 97757 96062 94733 93633 92721 92061 91526 91143 90831 90585 90419 90288 90200 90120 90068 90035
result:
wrong answer 2nd numbers differ - expected: '662', found: '584'