QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#735262 | #5312. Levenshtein Distance | Others | WA | 793ms | 26604kb | C++14 | 3.8kb | 2024-11-11 18:47:34 | 2024-11-11 18:47:34 |
Judging History
answer
#include <bits/stdc++.h>
#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 (0x3f3f3f3f)
#define il inline
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,vis[K],ans[K];
char a[N],b[N];
ull base[N*2],has[N*2];
int lg[N*2],sa[N*2],rk[N*2],nrk[N*2],st[20][N*2],id[N*2],l;
char c[N*2];
pai pos[N*2];
bool cmp(int x,int y) {
return c[x]<c[y];
}
bool cmp2(int x,int y) {
return pos[x]<pos[y];
}
struct Yanami {
il void init() {
l=0;
for(int i=1;i<=n;i++) c[++l]=a[i];
c[++l]='#';
for(int i=1;i<=m;i++) c[++l]=b[i];
base[0]=1;
for(int i=1;i<=l;i++) base[i]=base[i-1]*ba;
for(int i=1;i<=l;i++) has[i]=(has[i-1]*ba+c[i]);
for(int i=2;i<=l;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=l;i++) id[i]=i;
sort(id+1,id+l+1,cmp);
int cnt=0;
for(int i=1;i<=l;i++) rk[id[i]]=(cnt+=(c[id[i]]!=c[id[i-1]]));
// printf("init : ");for(int j=1;j<=l;j++) printf("%d ",rk[j]);puts("");
for(int i=0;i<=lg[l];i++) {
for(int j=1;j<=l;j++) pos[j]=pai(rk[j],rk[j+(1<<i)]),id[j]=j;
sort(id+1,id+l+1,cmp2);
cnt=0;
for(int j=1;j<=l;j++) rk[id[j]]=(cnt+=(pos[id[j]]!=pos[id[j-1]]));
// printf("%d : ",i);for(int j=1;j<=l;j++) printf("%d ",rk[j]);puts("");
}
// printf("%s",c+1);puts("");
for(int i=1;i<=l;i++) sa[rk[i]]=i;
// for(int i=1;i<=l;i++) printf("%d ",sa[i]);puts("");
for(int i=1;i<l;i++) st[0][i]=getlen(sa[i],sa[i+1]);//,printf("%d ",st[0][i]);puts("");
for(int i=1;i<=lg[l];i++)
for(int j=1;j+(1<<i)-1<=l;j++)
st[i][j]=min(st[i-1][j],st[i-1][j+(1<<i-1)]);
return ;
}
il int ask(int l,int r,ull *has) {
return (has[r]-has[l-1]*base[r-l+1]);
}
il int getlen(int x,int y) {
int l=0,r=min(::l-x+1,::l-y+1),mid;
while(l<r) {
mid=l+r+1>>1;
if(ask(x,x+mid-1,has)==ask(y,y+mid-1,has)) l=mid;
else r=mid-1;
}
return l;
}
il int getmin(int l,int r) {
int Lg=lg[r-l+1];
return min(st[Lg][l],st[Lg][r-(1<<Lg)+1]);
}
il int ask(int x,int y) {
return getmin(rk[x],rk[y]-1);
}
}LCP;
il 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: 2ms
memory: 11916kb
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: 793ms
memory: 26604kb
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: 707ms
memory: 26184kb
input:
30 BBAAABAABBBA ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...
output:
66080 150445 173518 193860 197151 196211 185448 182552 181672 181053 180655 180409 90280 90171 90097 90046 90012 89988 89977 89974 89971 89969 89967 89966 89965 89964 89963 89962 89961 89960 89959
result:
wrong answer 1st numbers differ - expected: '17', found: '66080'