QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#735115 | #5312. Levenshtein Distance | tx344 | TL | 1ms | 4432kb | C++14 | 2.4kb | 2024-11-11 17:31:35 | 2024-11-11 17:31:35 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define Int __int128
#define ld double
#define fi first
#define se second
#define PII pair<int,int>
#define PLI pair<ll,int>
#define PLL pair<ll,ll>
using namespace std;
const int N=1e5+5,M=35,Mod=1e9+7,Base=71;
int n,m,k,f[M][M*2],pw[N],ha[N],hb[N],ans[M];
char a[N],b[N];
void Max(int &x,int y){x=x<y?y:x;}
int ID(char x){return x>='a'&&x<='z'?x-'a':x>='A'&&x<='Z'?x-'A'+26:x-'0'+52;}
void Init()
{
pw[0]=1;
for(int i=1;i<N;i++)pw[i]=(ll)pw[i-1]*Base%Mod;
for(int i=1;i<=n;i++)ha[i]=((ll)ha[i-1]*Base+ID(a[i]))%Mod;
for(int i=1;i<=m;i++)hb[i]=((ll)hb[i-1]*Base+ID(b[i]))%Mod;
}
int Get1(int x,int y){return (ha[y]+(ll)(Mod-ha[x-1])*pw[y-x+1])%Mod;}
int Get2(int x,int y){return (hb[y]+(ll)(Mod-hb[x-1])*pw[y-x+1])%Mod;}
int LCP(int x,int y)
{
int l=1,r=min(n-x,m-y)+1;
while(l<=r)
{
int mid=l+r>>1;
if(Get1(x,x+mid-1)==Get2(y,y+mid-1))l=mid+1;
else r=mid-1;
}
return r;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
scanf("%d%s%s",&k,a+1,b+1),n=strlen(a+1),m=strlen(b+1);
Init();
// printf("LCP : %d\n",LCP(2,5));
for(int i=1;i<=m;i++)
{
memset(f,-63,sizeof(f));
f[0][M]=0;
for(int x=0;x<k;x++)
{
for(int y=M-x;y<=M+x;y++)if(f[x][y]>=0)
{
f[x][y]+=LCP(f[x][y]+1,i-1+f[x][y]+y-M+1);
// printf("%d %d %d %d %d\n",i,x,y-M,f[x][y],i-1+f[x][y]+y-M);
Max(f[x+1][y],f[x][y]);
Max(f[x+1][y-1],f[x][y]);
Max(f[x+1][y+1],f[x][y]);
if(f[x][y]<n&&i-1+f[x][y]+y-M<m)Max(f[x+1][y],f[x][y]+1);
if(f[x][y]<n)Max(f[x+1][y-1],f[x][y]+1);
if(i-1+f[x][y]+y-M<m)Max(f[x+1][y+1],f[x][y]);
}
}
for(int x=0;x<=k;x++)for(int y=M-x;y<=M+x;y++)if(f[x][y]>=0)
{
f[x][y]+=LCP(f[x][y]+1,i-1+f[x][y]+y-M+1);
// printf("%d %d %d %d %d\n",i,x,y-M,f[x][y],i-1+f[x][y]+y-M);
if(f[x][y]!=n||n+y-M<=0||i-1+n+y-M>m)continue;
// if(x==k)printf("%d %d [%d,%d]\n",x,y-M,i,i-1+n+y-M);
++ans[x];
}
}
for(int i=0,las=0;i<=k;i++)printf("%d\n",ans[i]-las),las=ans[i];
fprintf(stderr,"%.15lf\n",(ld)clock()/CLOCKS_PER_SEC);
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4432kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: -100
Time Limit Exceeded
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...