【题解】P13549 热辣滚烫
我们发现,若插入字符串,使得
那么预处理两个串的哈希值,枚举每个插入
注意特殊判断:
Code
#include<bits/stdc++.h>
typedef int IT;
typedef long long LL;
typedef __int128 int128;
typedef double DB;
typedef long double LDB;
#define pb push_back
#define fst first
#define sec second
#define psh push
#define mkp make_pair
#define PII pair<IT,IT>
#define PLI pair<LL,IT>
#define lowbit(x) ((x)&(-x))
using namespace std;
const int N=1e5+10,M=N;
const LL base=131,P=998244353;
int n,m;
char s1[N],s2[M];
LL hsh[2][N];
LL pw[N];
void init(int R){
pw[0]=1;
for(int i=1;i<=R;i++) pw[i]=pw[i-1]*base%P;
return;
}
LL gethsh(int l,int r,int id){
if(l>r) return 0;
int len=r-l+1;
return (hsh[id][r]-hsh[id][l-1]*pw[len]%P+P)%P;
}
void sl_reset();
void solve(){
sl_reset();
scanf("%d %d",&n,&m);
scanf("%s %s",s1+1,s2+1);
for(int i=1;i<=n;i++) hsh[0][i]=(hsh[0][i-1]*base%P+(s1[i]-'a'))%P;
for(int i=1;i<=m;i++) hsh[1][i]=(hsh[1][i-1]*base%P+(s2[i]-'a'))%P;
if(n==m){
if(hsh[0][n]!=hsh[1][m]){
printf("No\n");
}
else printf("Yes\n");
return;
}
bool fg=0;
for(int i=1;i<=n;i++){
if(gethsh(1,i,0)==gethsh(1,i,1)){
if(gethsh(i+1,n,0)==gethsh(m-(n-i)+1,m,1)){
fg=1;
break;
}
}
}
if(gethsh(1,n,0)==gethsh(m-n+1,m,1)){
fg=1;
}
if(gethsh(1,n,0)==gethsh(1,n,1)){
fg=1;
}
if(fg){
printf("Yes\n");
}
else printf("No\n");
return;
}
void sl_reset(){
return;
}
int main(){
init(1e5);
int T=1;
scanf("%d",&T);
while(T--){
solve();
}
return 0;
}
link