QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#390448 | #5312. Levenshtein Distance | OIer2008 | RE | 1ms | 10024kb | C++14 | 2.6kb | 2024-04-15 15:44:20 | 2024-04-15 15:44:20 |
Judging History
answer
#include<bits/stdc++.h>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define fo(i,l,r) for(int i=l;i<=r;++i)
#define fd(i,r,l) for(int i=r;i>=l;--i)
#define ll long long
#define V inline void
#define I inline int
#define B inline bool
#define L inline ll
#define vi vector<int>
#define vii vector<pi>
#define pi pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
using namespace std;
const int N=2e5+10,M=31;
I read() {
int x=0,y=1;char c=getchar();
while(c<48||c>57){if(c==45)y=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*y;
}
int rk[N<<1],oldrk[N<<1],sa[N],id[N],cnt[N],n,k,f[M][M<<1],sum[M];
int l1,l2,h[N][20],lg[N];
ll ans[M];
char c[N],d[N];
V init() {
l1=strlen(c+1),l2=strlen(d+1);n=l1+l2+1;
c[l1+1]='z'+1;
fo(i,1,l2) c[l1+1+i]=d[i];
int m=27;
fo(i,1,n) cnt[rk[i]=c[i]-'a'+1]++;
fo(i,1,m) cnt[i]+=cnt[i-1];
fd(i,n,1) sa[cnt[rk[i]]--]=i,oldrk[i]=rk[i];
int k=0;
fo(i,1,n) {
if(oldrk[sa[i]]==oldrk[sa[i-1]]) rk[sa[i]]=k;
else rk[sa[i]]=++k;
}
m=k;
for(int w=1;w<n;w<<=1) {
int k=0;
fd(i,n,n-w+1) id[++k]=i;
fo(i,1,n) if(sa[i]>w) id[++k]=sa[i]-w;
fo(i,1,m) cnt[i]=0;
fo(i,1,n) cnt[rk[i]]++,oldrk[i]=rk[i];
fo(i,1,m) cnt[i]+=cnt[i-1];
fd(i,n,1) sa[cnt[rk[id[i]]]--]=id[i];
k=0;
fo(i,1,n) {
if(oldrk[sa[i]]==oldrk[sa[i-1]]&&oldrk[sa[i]+w]==oldrk[sa[i-1]+w]) rk[sa[i]]=k;
else rk[sa[i]]=++k;
}
m=k;if(m==n) break;
}
// fo(i,1,n) printf("%d ",sa[i]);puts("");
k=0;lg[0]=-1;
fo(i,1,n) {
if(k) k--;
while(c[i+k]==c[sa[rk[i]-1]+k]) ++k;
h[rk[i]][0]=k;lg[i]=lg[i>>1]+1;
}
fo(i,1,lg[n]) fo(j,1,n-(1<<i)+1) h[j][i]=min(h[j][i-1],h[j+(1<<i-1)][i-1]);
}
I lcp(int x,int y) {
if(x>l1||y>l2) return 0;
y+=l1+1;x=rk[x];y=rk[y];if(x>y) swap(x,y);
x++;int k=lg[y-x+1];
return min(h[x][k],h[y-(1<<k)+1][k]);
}
V upd(int &x,int y) {
if(y>x) x=y;
}
int main() {
k=read();scanf("%s%s",c+1,d+1);
init();n=l2;
fo(st,1,n) {
fo(i,0,k) fo(j,0,k*2) f[i][j]=-1e9;
fo(i,0,k*2) sum[i]=-1;f[0][k]=lcp(1,st);
fo(i,0,k) fo(j,-i,i) if(f[i][j+k]>=0) {
// printf("! %d %d : %d\n",i,j,f[i][j+k]);
int t=f[i][j+k];
if(t==l1) {
if(sum[j+k]==-1) sum[j+k]=i;
}
if(t+1<=l1) upd(f[i+1][j+k-1],t+1+lcp(t+2,t+j+1+st-1));// Add
else if(t+j>0) upd(f[i+1][j+k-1],t);
if(t+1<=l1&&t+j+1+st-1<=l2) upd(f[i+1][j+k],t+1+lcp(t+2,t+j+2+st-1));// Change
if(t+j+1+st-1<=l2) upd(f[i+1][j+k+1],t+lcp(t+1,t+j+2+st-1));// Remove
}
// exit(0);
fo(i,max(-k,1-l1),k) if(sum[k+i]!=-1) ans[sum[k+i]]++;
}
fo(i,0,k) printf("%lld\n",ans[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 10024kb
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...