QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390195 | #5312. Levenshtein Distance | Xun_xiaoyao | RE | 1ms | 6712kb | C++14 | 3.3kb | 2024-04-15 09:57:20 | 2024-04-15 09:57:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int Qread()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
return x;
}
int get_str(char *S)
{
int len=1;
do S[1]=getchar();while(S[1]<' '||S[1]>'~');
do S[++len]=getchar();while(S[len]>=' '&&S[len]<='~');
return len-1;
}
int k,lenS,lenT;
char S[100010],T[100010],ST[200010];
int sa[200010],rk[400010],rk_[400010],cnt[200010],id[200010];
int h[200010],st[20][200010],Log[200010];
void SA(int n)
{
for(int i=1;i<=n;i++)
sa[i]=i,rk[i]=ST[i];
int m=max(n,300);
for(int i=1;i<=n;i++) cnt[rk[i]]++;
for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for(int i=n;i;i--) sa[cnt[rk[i]]--]=i;
for(int w=1;w<n;w<<=1)
{
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++) id[i]=sa[i];
for(int i=1;i<=n;i++) cnt[rk[id[i]+w]]++;
for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--) sa[cnt[rk[id[i]+w]]--]=id[i];
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++) id[i]=sa[i];
for(int i=1;i<=n;i++) cnt[rk[id[i]]]++;
for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--) sa[cnt[rk[id[i]]]--]=id[i];
memcpy(rk_,rk,n<<3);
for(int i=1,p=0;i<=n;i++)
{
if(rk_[sa[i]]!=rk_[sa[i-1]]||rk_[sa[i]+w]!=rk_[sa[i-1]+w]) p++;
rk[sa[i]]=p;
}
}
for(int i=1;i<=n;i++) rk[sa[i]]=i;
for(int i=1;i<=n;i++)
{
h[i]=max(0,h[i-1]-1);
if(rk[i]!=1) while(ST[i+h[i]]==ST[sa[rk[i]-1]+h[i]]) h[i]++;
}
for(int i=2;i<=n;i++) Log[i]=Log[i>>1]+1;
for(int i=1;i<=n;i++) st[0][rk[i]]=h[i];
for(int i=1;i<=Log[n];i++) for(int j=1;j+(1<<i)-1<=n;j++)
st[i][j]=min(st[i-1][j],st[i-1][j+(1<<i-1)]);
}
//get the LCS of S[l,lenS] and T[r,lenT]
int qry(int l,int r)
{
if(l>lenS||r>lenT||l<0||r<0) return 0;
l=rk[l],r=rk[r+lenS+1];
if(l>r) swap(l,r);
l++;
int t=Log[r-l+1];
return min(st[t][l],st[t][r-(1<<t)+1]);
}
int f[30][60];
int ans[110];
int main()
{
k=Qread();
lenS=get_str(S),lenT=get_str(T);
for(int i=1;i<=lenS;i++) ST[i]=S[i];
ST[lenS+1]='~';
for(int i=1;i<=lenT;i++) ST[i+lenS+1]=T[i];
SA(lenS+1+lenT);
for(int i=1;i<=lenT;i++)
{
memset(f,0xcf,sizeof(f));
for(int lim=0;lim<=k;lim++)
{
if(lim>0)
{
f[lim][-lim+k]=f[lim-1][1-lim+k]+1;
f[lim][lim+k]=f[lim-1][lim-1+k];
// if(i==1) printf("%d %d %d\n",lim,f[lim][lim+k],qry(f[lim][lim+k]+1,i+f[lim][lim+k]+lim));
// if(lim==1) printf("%d (%d %d)%d\n",f[lim][-lim+k],f[lim][-lim+k]+1,i+f[lim][lim+k]-lim,qry(f[lim][-lim+k]+1,i+f[lim][lim+k]-lim));
f[lim][-lim+k]=f[lim][-lim+k]+qry(f[lim][-lim+k]+1,i+f[lim][-lim+k]-lim);
f[lim][lim+k]=f[lim][lim+k]+qry(f[lim][lim+k]+1,i+f[lim][lim+k]+lim);
}
else f[0][k]=qry(1,i);
for(int d=1-lim;d<lim;d++)
{
f[lim][d+k]=max({f[lim-1][d-1+k],f[lim-1][d+k]+1,f[lim-1][d+1+k]+1});
f[lim][d+k]=f[lim][d+k]+qry(f[lim][d+k]+1,i+f[lim][d+k]+d);
}
// if(i==2)
// {
// printf("%d:",lim);
// for(int d=-k;d<=k;d++)
// printf("%d ",f[lim][d+k]);
// printf("\n");
// }
}
for(int d=max(-lenS+1,-k);d<=k&&i+d+lenS-1<=lenT;d++)
for(int j=0;j<=k;j++)
if(f[j][d+k]>=lenS)
{
// printf("(%d %d)",d,j);
ans[j]++;
break;
}
// printf("\n");
// printf("%d:\n",i);
// for(int j=0;j<=k;j++)
// printf("%d ",ans[j]);
// printf("\n");
}
for(int i=0;i<=k;i++)
printf("%d\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6712kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: -100
Runtime Error
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...