UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#210927#2068. 最长公共前缀drdilyor11271ms193580kbC++113.1kb2024-08-08 11:01:132024-08-08 12:39:24

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
void ts(){cout<<"IAKIOI\n";}
inline int read(){
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
const int mod=998244353,base=1145141,base2=1919839;
struct mint{
    int x;mint(int o=0){x=o;}mint&operator+=(mint a){return(x+=a.x)%=mod,*this;}mint&operator-=(mint a){return(x+=mod-a.x)%=mod,*this;}
    mint&operator*=(mint a){return(x=1ll*x*a.x%mod),*this;}mint&operator^=( int b){mint a=*this;x=1;while(b)(b&1)&&(*this*=a,1),a*=a,b>>=1;return*this;}
    mint&operator/=(mint a){return*this*=(a^=mod-2);}friend mint operator+(mint a,mint b){return a+=b;}friend mint operator-(mint a,mint b){return a-=b;}
    friend mint operator*(mint a,mint b){return a*=b;}friend mint operator/(mint a,mint b){return a/=b;}friend mint operator^(mint a,int b){return a^=b;}
    mint operator-(){return 0-*this;}bool operator==(const mint b)const{return x==b.x;}
};
int n,m,q;
char s[2005][2005];
mint pw[4005][2];
mint d[2005][2005][2],r[2005][2005][2],h[2005][2005][2];
pair<mint,mint> qr(int x,int y,int len){
	return (pair<mint,mint>){(r[x][y+len-1][0]-r[x][y-1][0])/pw[y-1][0],(r[x][y+len-1][1]-r[x][y-1][1])/pw[y-1][1]};
}
pair<mint,mint> qd(int x,int y,int len){
	return (pair<mint,mint>){(d[y][x+len-1][0]-d[y][x-1][0])/pw[x-1][0],(d[y][x+len-1][1]-d[y][x-1][1])/pw[x-1][1]};
}
pair<mint,mint> qh(int x,int y,int len){
	return (pair<mint,mint>){(h[x+len-1][y+len-1][0]-h[x-1][y-1][0])/pw[min(x-1,y-1)][0],(h[x+len-1][y+len-1][1]-h[x-1][y-1][1])/pw[min(x-1,y-1)][1]};
}
int get(int a,int b,char c){
	if(c=='H')return m-b+1;
	if(c=='V')return n-a+1;
	return min(n-a+1,m-b+1);
}
void write(int x){
	if(x<10){putchar(x+'0');return;}
	write(x/10);
	putchar(x%10+'0');
}
pair<mint,mint> qry(int a,int b,char c,int len){
	if(c=='H')return qr(a,b,len);
	if(c=='V')return qd(a,b,len);
	return qh(a,b,len);
}
signed main(){
	n=read(),m=read(),q=read();
	for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
	pw[0][0]=pw[0][1]=1;
	for(int i=1;i<=4000;i++)pw[i][0]=pw[i-1][0]*base,pw[i][1]=pw[i-1][1]*base2;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			r[i][j][0]=r[i][j-1][0]+pw[j][0]*(s[i][j]-'a'+1);
			r[i][j][1]=r[i][j-1][1]+pw[j][1]*(s[i][j]-'a'+1);
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			d[i][j][0]=d[i][j-1][0]+pw[j][0]*(s[j][i]-'a'+1);
			d[i][j][1]=d[i][j-1][1]+pw[j][1]*(s[j][i]-'a'+1);
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			h[i][j][0]=h[i-1][j-1][0]+pw[min(i,j)][0]*(s[i][j]-'a'+1);
			h[i][j][1]=h[i-1][j-1][1]+pw[min(i,j)][1]*(s[i][j]-'a'+1);
		}
	}
	while(q--){
		int a,b,x,y;
		char c,z;
		a=read(),b=read();
		c=getchar();
		while(c!='H'&&c!='V'&&c!='D')c=getchar();
		x=read(),y=read();
		z=getchar();
		while(z!='H'&&z!='V'&&z!='D')z=getchar();
		int l=0,r=min(get(a,b,c),get(x,y,z));
		int res=0;
		while(l<=r){
			int mid=(l+r)/2;
			if(qry(a,b,c,mid)==qry(x,y,z,mid))res=mid,l=mid+1;
			else r=mid-1;
		}
		write(res),putchar('\n');
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 11
Accepted

Test #1:

score: 11
Accepted
time: 134ms
memory: 191624kb

input:

1000 2000 10000
awanannwnnaaawwnwawanwnwaaaanwnaaananwawwwwnannannawwwawwaaaaannwnwnwnnaaawaawawannn...

output:

0
0
0
0
0
0
0
2
0
1
0
0
1
0
1
0
2
0
1
2
0
0
2
0
1
1
0
0
0
0
1
0
0
0
1
0
0
1
0
1
1
0
2
0
1
3
1
0
3
0
...

result:

ok 10000 tokens

Test #2:

score: 0
Accepted
time: 137ms
memory: 193580kb

input:

2000 1000 10000
zzqzqqqqqzqqqqzqqqqzqzqzzzqqzqqqzzqzzqqqqqqqqqqqzzqzqqqzzqqzzqzqzzzqqzqqzzzzzzqzzzqq...

output:

0
3
3
1
0
2
2
2
1
0
1
0
0
0
1
0
2
0
6
1
0
1
1
0
2
1
1
1
2
4
0
1
3
2
1
0
1
2
0
0
0
1
1
0
1
1
0
4
0
3
...

result:

ok 10000 tokens

Subtask #2:

score: 0
Time Limit Exceeded

Test #3:

score: 0
Time Limit Exceeded

input:

2000 2000 1000000
gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...

output:

1146
541
203
322
618
166
345
138
520
206
1031
667
741
921
361
1110
1057
372
899
209
491
69
93
639
14...

result:


Subtask #3:

score: 0
Skipped

Subtask #4:

score: 0
Skipped