#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define frep(i,a,b) for(int i=a;i>=b;--i)
typedef unsigned long long ll;
const int N = 100007;
const ll base = 107;
using namespace std;
int n,q;
ll P[11][N], h[11][N], mod[11];
int cc;
char s[N];
void init_hash() {
cc=0;
mod[++cc] = 1e9+7;
mod[++cc] = 1000007;
mod[++cc] = 998244353;
mod[++cc] = 100007;
P[0][0]=1; rep(i,1,100) P[0][i]=P[0][i-1]*base;
frep(i,n,1) h[0][i] = h[0][i+1]*base + s[i];
rep(i,1,cc){
P[i][0]=1;
rep(j,1,100) P[i][j]=(P[i][j-1]*base)%mod[i];
}
rep(i,1,cc)frep(j,n,1)h[i][j] = ((h[i][j+1]*base)%mod[i]+s[j])%mod[i];
}
ll ask0(int x,int y) {
if(x>y)swap(x,y);
return h[0][x]-h[0][y+1]*P[0][y-x+1];
}
ll ask1(int x,int y,int z) {
if(x>y)swap(x,y);
return (h[z][x]%mod[z]-(h[z][y+1]*P[z][y-x+1])%mod[z]+mod[z])%mod[z];
}
int ck(int x,int y,int L) {
if(ask0(x,x+L-1)!=ask0(y,y+L-1))return 0;
rep(i,1,cc){
if(ask1(x,x+L-1,i)!=ask1(y,y+L-1,i)) return 0;
}
return 1;
}
int solve(int x,int y) {
int l=1,r=min(n-x+1,n-y+1),mid,ans=0;
if(s[x]!=s[y])return 0;
while(l<=r) {
mid = (l+r)>>1;
if(ck(x,y,mid))l=mid+1,ans=mid;
else r=mid-1;
}
return ans;
}
int main() {
scanf(" %s",s+1);
n=strlen(s+1);
init_hash();
scanf("%d",&q);
while(q--) {int x,y;
scanf("%d %d",&x,&y);
printf("%d\n",solve(x,y));
}
return 0;
}
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define frep(i,a,b) for(int i=a;i>=b;--i)
typedef unsigned long long ll;
const int N = 50100;
const ll base = 107;
using namespace std;
int n,q,L,X;
ll P[211], h[N][211],a[N];
char s[211];
ll cal(int s,int x) {
return h[s][x-1]*P[L-x+1] +(h[s][L]-h[s][x]*P[L-x]);
}
int main() {
scanf("%d%d%d",&n,&L,&X);
P[0]=1;rep(i,1,L)P[i]=P[i-1]*base;
rep(i,1,n) {
scanf(" %s",s+1);
rep(j,1,L) h[i][j]=h[i][j-1]*base+s[j];
}
int ans=0;
rep(i,1,L){
rep(j,1,n)a[j]=cal(j,i);
sort(a+1,a+n+1);
int tmp = 0;
rep(j,2,n){
if(a[j]!=a[j-1])tmp=0;
else ++tmp;
ans += tmp;
}
}
printf("%d\n",ans);
return 0;
}
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef unsigned long long ll;
const int N = 1007;
const ll base1 = 107;
const ll base2 = 31;
const ll mod = 10000007;
using namespace std;
int n,m,a,b,q;
ll P1[N], P2[N], h[N][N];
bool vis[mod+1];
char mp[N][N];
void init_hash() {
P1[0]=1; rep(i,1,100) P1[i]=P1[i-1]*base1;
P2[0]=1; rep(i,1,100) P2[i]=P2[i-1]*base2;
rep(i,1,n)rep(j,1,m) h[i][j] += h[i-1][j]*base1 + mp[i][j];
rep(i,1,n)rep(j,1,m) h[i][j] += h[i][j-1]*base2;
}
ll ask(int x,int y,int xl,int yl) {
return h[x][y]-h[x-xl][y]*P1[xl]-h[x][y-yl]*P2[yl]+h[x-xl][y-yl]*P1[xl]*P2[yl];
}
int main() {
scanf("%d%d%d%d",&n,&m,&a,&b);
rep(i,1,n)scanf(" %s",mp[i]+1);
init_hash();
rep(i,a,n)rep(j,b,m) vis[ask(i,j,a,b)%mod]=1;
scanf("%d",&q);
while(q--) {
rep(i,1,a)scanf(" %s",mp[i]+1);
memset(h,0,sizeof(h));
rep(i,1,a)rep(j,1,b)h[i][j]+=h[i-1][j]*base1+mp[i][j];
rep(i,1,a)rep(j,1,b)h[i][j]+=h[i][j-1]*base2;
printf("%d\n",vis[h[a][b]%mod]);
}
return 0;
}
原文:https://www.cnblogs.com/RRRR-wys/p/9256622.html