QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738109 | #5312. Levenshtein Distance | yanshanjiahong | TL | 0ms | 0kb | C++14 | 4.1kb | 2024-11-12 17:45:49 | 2024-11-12 17:45:50 |
answer
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define mp make_pair
#define sec second
#define fir first
#define pii pair<int,int>
#define lowbit(i) i&-i
#define double long double
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+5,M=35,S=(1<<8)+5,inf=(ll)1e18+7;
const double eps=1e-8;
void read(int &p){
int x=0,w=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-')w=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
p=x*w;
}
int n,m,k;
int s[N],t[N],sl[N];
int f[M][M*2];
int sa[N],tp[N],rk[N],tax[N],ht[N];
bool cmps(int x,int y){
return rk[x]<rk[y];
}
int tot;
void qsort(){
int upp=max(128ll,n+m+1);
rep(i,0,upp)
tax[i]=0;
rep(i,1,tot)
tax[rk[i]]++;
rep(i,1,upp)
tax[i]+=tax[i-1];
repp(i,tot,1)
sa[tax[rk[tp[i]]]--]=tp[i];
}
void sa_sort(){
rep(i,1,n+m+1)
rk[i]=sl[i],sa[i]=i;
sort(sa+1,sa+n+m+2,cmps);
for(int w=1;w;w*=2){
int cnt=0;
rep(i,tot-w+1,tot)
tp[++cnt]=i;
rep(i,1,tot)
if(sa[i]-w>=1)tp[++cnt]=sa[i]-w;
qsort();
memcpy(tp,rk,sizeof(rk));
cnt=1,rk[sa[1]]=1;
rep(i,2,tot){
if(tp[sa[i]]==tp[sa[i-1]]&&sa[i]+w<=tot&&sa[i-1]+w<=tot&&tp[sa[i]+w]==tp[sa[i-1]+w])rk[sa[i]]=cnt;
else rk[sa[i]]=++cnt;
}
if(cnt>=tot)break;
}
}
void getht(){
int len=0;
rep(i,1,tot){
if(rk[i]==1)continue;
int j=sa[rk[i]-1];
if(len)len--;
while(j+len<=tot&&i+len<=tot&&sl[i+len]==sl[j+len])
len++;
ht[rk[i]]=len;
}
}
struct st_form{
int lg[N],st[N][20];
void init(){
lg[1]=0;
rep(i,2,tot)
lg[i]=lg[i/2]+1;
}
void build(){
init();
rep(i,1,tot)
st[i][0]=ht[i];
rep(j,1,18){
rep(i,1,tot){
st[i][j]=st[i][j-1];
if(i+(1<<(j-1))<=tot)st[i][j]=min(st[i][j],st[i+(1<<(j-1))][j-1]);
}
}
}
int query(int x,int y){
int len=y-x+1;
return min(st[x][lg[len]],st[y-(1<<lg[len])+1][lg[len]]);
}
}ST;
int getlcp(int x,int y){//S:x,T:y
//printf("!%lld %lld:%lld\n",x,y,ST.query(rk[x]+1,rk[y+1+n]));
return ST.query(rk[x]+1,rk[y+1+n]);
}
int ans[N],val[M*2];
int solve(int tst){
rep(i,0,k){
rep(j,0,2*k)
f[i][j]=-inf;
}
f[0][k]=0;
rep(i,0,k){
rep(j,0,2*k){
if(f[i][j]<0)continue;
//printf("!f:%lld %lld,%lld\n",i,j,f[i][j]);
int spos=f[i][j],tpos=tst-1+(f[i][j]+(j-k));
if(spos<n&&tpos<m)f[i][j]+=getlcp(spos+1,tpos+1);
if(i==k)continue;
//delete S
//printf("!!!%lld %lld\n",j,f[i][j]);
if(j)f[i+1][j-1]=max(f[i+1][j-1],min(n,f[i][j]+1));
//delete T
f[i+1][j+1]=max(f[i+1][j+1],f[i][j]);
//modify to equal
f[i+1][j]=max(f[i+1][j],min(n,f[i][j]+1));
}
}
rep(i,0,2*k)
val[i]=inf;
rep(i,0,k){
rep(j,0,2*k){
if(f[i][j]!=n)continue;
int len=n+(j-k);
if(tst+len-1>m||len<=0)continue;
val[j]=min(val[j],i);
}
}
repp(i,2*k,k)
if(i!=2*k)val[i]=min(val[i],val[i+1]);
rep(i,0,k)
if(i!=0)val[i]=min(val[i],val[i-1]);
rep(i,0,2*k){
int len=n+(i-k);
if(len>0&&tst+len-1<=m&&val[i]<=k)ans[val[i]]++;
}
/*rep(i,0,k){
printf("ans:%lld %lld\n",i,ans[i]);
}*/
}
signed main(){
read(k);
string st;
cin>>st,n=st.size();
rep(i,1,n)
s[i]=st[i-1],sl[i]=s[i];
cin>>st,m=st.size();
sl[n+1]=1,tot=n+m+1;
rep(i,1,m)
t[i]=st[i-1],sl[i+n+1]=t[i];
sa_sort(),getht();
ST.build();
rep(i,1,m)
solve(i);
rep(i,0,k)
printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
4 aaa aabbaab