QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#735129 | #5312. Levenshtein Distance | Others | TL | 0ms | 3796kb | C++14 | 2.5kb | 2024-11-11 17:39:24 | 2024-11-11 17:39:25 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define int long long
#define ull unsigned ll
#define eps 1e-12
#define db long double
#define ull unsigned long long
#define wr(x,ch) write(x),putchar(ch)
#define lb(x) ((x)&-(x))
#define inf (0x3f3f3f3f3f3f3f3f)
using namespace std;
typedef pair<ll,ll> pai;
#define x first
#define y second
ll read() {
ll 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;
}
void write(ll x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);
putchar(x%10^48);
return ;
}
const int N=1e5+5,K=65,ba=10086001,mod=1e9+9;
int f[K][K],k,n,m,ans[K],vis[K];
char a[N],b[N];
struct Yanami {
ull base[N],ha[N],hb[N];
void init() {
base[0]=1;
for(int i=1;i<=max(n,m);i++) base[i]=base[i-1]*ba;
for(int i=1;i<=n;i++) ha[i]=(ha[i-1]*ba+a[i]);
for(int i=1;i<=m;i++) hb[i]=(hb[i-1]*ba+b[i]);
return ;
}
inline int ask(int l,int r,ull *has) {
return (has[r]-has[l-1]*base[r-l+1]);
}
int ask(int x,int y) {
int l=0,r=min(n-x+1,m-y+1),mid;
while(l<r) {
mid=l+r+1>>1;
if(ask(x,x+mid-1,ha)==ask(y,y+mid-1,hb)) l=mid;
else r=mid-1;
}
return l;
}
}LCP;
void chkmax(int &x,int y) {
if(y>x) x=y;
return ;
}
signed main() {
k=read();
scanf("%s%s",a+1,b+1);
n=strlen(a+1),m=strlen(b+1);
LCP.init();
for(int l=1;l<=m;l++) {
for(int i=0;i<=k;i++) {
for(int j=0;j<=2*k;j++)
f[i][j]=-inf,vis[j]=0;
}
f[0][k]=LCP.ask(1,l);
// printf("%d::\n",l);
for(int i=0;i<=k;i++) {
for(int j=1;j<=2*k-1;j++) {
if(f[i][j]<0) continue;
int x=f[i][j],y=l+f[i][j]+(j-k)-1;
if(x<n&&y<m) chkmax(f[i+1][j],f[i][j]+LCP.ask(x+2,y+2)+1);
if(x<n&&y<m-1&&LCP.ask(x+1,y+2)) chkmax(f[i+1][j+1],f[i][j]+LCP.ask(x+1,y+2));
if(x<n) chkmax(f[i+1][j-1],f[i][j]+LCP.ask(x+2,y+1)+1);
// if(x==n&&y<m) chkmax(f[i+1][j+1],f[i][j]);
}
for(int j=0;j<2*k;j++) {
if(l+f[i][j]+(j-k)-1<m)
chkmax(f[i+1][j+1],f[i][j]);
chkmax(f[i+1][j-1],f[i][j]);
}
for(int j=0;j<=2*k;j++)
if(vis[j]) f[i][j]=-inf;
else if(f[i][j]==n) vis[j]=1;
// printf("%d : ",i);
// for(int j=0;j<=2*k;j++) printf("%4d ",f[i][j]==-inf?-1:f[i][j]);puts("");
for(int j=0;j<=2*k;j++)
if(f[i][j]==n&&k-j<n)
ans[i]++;
}
}
// wr(ans[k],'\n');
// puts("----------");
for(int i=0;i<=k;i++) wr(ans[i],'\n');
return 0;
}
/*
g++ 1.cpp -o 1.exe -std=c++14 -O2;./1.exe
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3796kb
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...