QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#738154#5312. Levenshtein DistanceyanshanjiahongWA 466ms33184kbC++234.1kb2024-11-12 17:57:102024-11-12 17:57:11

Judging History

你现在查看的是最新测评结果

  • [2024-11-12 17:57:11]
  • 评测
  • 测评结果:WA
  • 用时:466ms
  • 内存:33184kb
  • [2024-11-12 17:57:10]
  • 提交

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];
void 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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 20124kb

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: 433ms
memory: 33184kb

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: 466ms
memory: 32536kb

input:

30
BBAAABAABBBA
ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...

output:

66081
150459
171480
191979
198797
196832
185813
182882
181948
181261
180785
180500
90342
90223
90125
90059
90021
89993
89978
89974
89971
89969
89967
89966
89965
89964
89963
89962
89961
89960
89959

result:

wrong answer 1st numbers differ - expected: '17', found: '66081'