QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#368137#850. Edit Distance Yet AgainHarry27182WA 148ms10164kbC++144.1kb2024-03-26 21:04:502024-03-26 21:04:52

Judging History

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

  • [2024-03-26 21:04:52]
  • 评测
  • 测评结果:WA
  • 用时:148ms
  • 内存:10164kb
  • [2024-03-26 21:04:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int mod1=1000000007,mod2=1000000009,N=1000005,K=1005;
int hs1[N],hs2[N],ht1[N],ht2[N],p1[N],p2[N];char s[N],t[N];
vector<pair<int,int> >b[K],a[K];pair<int,int> val[K];
int n,m,k,T,vis[K<<1];
int qs1(int l,int r){return (hs1[r]-1ll*hs1[l-1]*p1[r-l+1]%mod1+mod1)%mod1;}
int qs2(int l,int r){return (hs2[r]-1ll*hs2[l-1]*p2[r-l+1]%mod2+mod2)%mod2;}
int qt1(int l,int r){return (ht1[r]-1ll*ht1[l-1]*p1[r-l+1]%mod1+mod1)%mod1;}
int qt2(int l,int r){return (ht2[r]-1ll*ht2[l-1]*p2[r-l+1]%mod2+mod2)%mod2;}
void print(int p,int q)
{
	if(p==0)return;
	for(int i=0;i<a[p-1].size();i++)
	{
		int x=0,y=0;
		if(a[p-1][i].first<n)
		{
			x=a[p-1][i].first+1;y=a[p-1][i].second;
			int l=1,r=min(n-x,m-y),pos=0;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(qs1(x+1,x+mid)==qt1(y+1,y+mid)&&qs2(x+1,x+mid)==qt2(y+1,y+mid))pos=mid,l=mid+1;
				else r=mid-1;
			}
			if(a[p][q]==make_pair(x+pos,y+pos))
			{
				val[p]=make_pair(i,1);print(p-1,i);
				return;
			}
		}
		if(a[p-1][i].second<m)
		{
			x=a[p-1][i].first;y=a[p-1][i].second+1;
			int l=1,r=min(n-x,m-y),pos=0;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(qs1(x+1,x+mid)==qt1(y+1,y+mid)&&qs2(x+1,x+mid)==qt2(y+1,y+mid))pos=mid,l=mid+1;
				else r=mid-1;
			}
			if(a[p][q]==make_pair(x+pos,y+pos))
			{
				val[p]=make_pair(i,2);print(p-1,i);
				return;
			}
		}
		if(a[p-1][i].first<n&&a[p-1][i].second<m)
		{
			x=a[p-1][i].first+1;y=a[p-1][i].second+1;
			int l=1,r=min(n-x,m-y),pos=0;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(qs1(x+1,x+mid)==qt1(y+1,y+mid)&&qs2(x+1,x+mid)==qt2(y+1,y+mid))pos=mid,l=mid+1;
				else r=mid-1;
			}
			if(a[p][q]==make_pair(x+pos,y+pos))
			{
				val[p]=make_pair(i,3);print(p-1,i);
				return;
			}
		}
	}
}
int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>k;cin>>(s+1)>>(t+1);
		p1[0]=p2[0]=1;
		for(int i=1;i<=max(n,m);i++)
		{
			p1[i]=1ll*p1[i-1]*131%mod1;
			p2[i]=1ll*p2[i-1]*131%mod2; 
		}
		for(int i=1;i<=n;i++)
		{
			hs1[i]=(1ll*hs1[i-1]*131%mod1+s[i])%mod1;
			hs2[i]=(1ll*hs2[i-1]*131%mod2+s[i])%mod2;
		}
		for(int i=1;i<=m;i++)
		{
			ht1[i]=(1ll*ht1[i-1]*131%mod1+t[i])%mod1;
			ht2[i]=(1ll*ht2[i-1]*131%mod2+t[i])%mod2;
		}
		b[0].emplace_back(make_pair(0,0));
		int flag=0;
		for(int i=0;i<=k;i++)
		{
			for(int j=0;j<b[i].size();j++)
			{
				int x=b[i][j].first,y=b[i][j].second;
				int l=1,r=min(n-x,m-y),pos=0;
				while(l<=r)
				{
					int mid=(l+r)>>1;
					if(qs1(x+1,x+mid)==qt1(y+1,y+mid)&&qs2(x+1,x+mid)==qt2(y+1,y+mid))pos=mid,l=mid+1;
					else r=mid-1;
				}
				a[i].emplace_back(make_pair(x+pos,y+pos));
			}
			vector<pair<int,int> >tmp;
			for(int j=-k;j<=k;j++)vis[j+k]=0;
			for(int j=a[i].size()-1;j>=0;j--)
			{
				if(vis[a[i][j].first-a[i][j].second+k])continue;
				vis[a[i][j].first-a[i][j].second+k]=1;
				tmp.emplace_back(a[i][j]);
			}
			swap(tmp,a[i]);
			//for(int j=0;j<a[i].size();j++)cout<<a[i][j].first<<' '<<a[i][j].second<<'\n';
			//cout<<'\n';
			for(int j=0;j<a[i].size();j++)
			{
				if(a[i][j].first==n&&a[i][j].second==m)
				{
					cout<<"YES\n";
					cout<<i<<'\n';flag=1;
					print(i,j);
					int num1=a[0][0].first,num2=a[0][0].second;
					for(int p=1;p<=i;p++)
					{
						int q=val[p].first,lq=val[p-1].first;
						int now1=a[p][q].first-a[p-1][lq].first,now2=a[p][q].second-a[p-1][lq].second;
						if(val[p].second==1)cout<<"DELETE "<<num1+1<<'\n',now1--;
						else if(val[p].second==2)cout<<"INSERT "<<num1+1<<' '<<t[a[p-1][lq].second+1]<<'\n',now2++;
						else cout<<"REPLACE "<<num1+1<<' '<<t[a[p-1][lq].second+1]<<'\n';
						num1+=now1;num2+=now2;		
					}
					break;
				}
			}
			if(flag==1)break;
			for(int j=0;j<a[i].size();j++)
			{
				if(a[i][j].first<n)b[i+1].emplace_back(make_pair(a[i][j].first+1,a[i][j].second));
				if(a[i][j].second<m)b[i+1].emplace_back(make_pair(a[i][j].first,a[i][j].second+1));
				if(a[i][j].first<n&&a[i][j].second<m)b[i+1].emplace_back(make_pair(a[i][j].first+1,a[i][j].second+1));
			}
		}
		if(flag==0)cout<<"NO\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 7940kb

input:

2
3 4 3
kot
plot
5 7 3
zycie
porazka

output:

YES
2
REPLACE 1 p
INSERT 2 l
NO

result:

ok OK!

Test #2:

score: -100
Wrong Answer
time: 148ms
memory: 10164kb

input:

100
100 100 0
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
16 7 100
vvvvvvvvvvvvvvvv
qydopaq
37 33 43
ggggggggggggggggggggggggggggggggggggg
osk...

output:

YES
0
YES
22
REPLACE 1 q
DELETE 2
DELETE 2
DELETE 0
DELETE 0
DELETE -2
DELETE -2
DELETE -4
DELETE -4
DELETE -5
INSERT -4 q
DELETE -4
INSERT -2 q
DELETE -1
INSERT 0 a
DELETE 1
INSERT 2 p
DELETE 3
INSERT 2 a
DELETE 3
INSERT 2 q
DELETE 3
NO
NO
YES
46
REPLACE 1 g
INSERT 2 f
INSERT 3 f
INSERT 3 f
INSERT ...

result:

wrong answer At test 1 expected 16 got 22