http://www.fjutacm.com/Contest.jsp?cid=705#P9
题意:
两
个
字
符
串
S
,
都
是
01
串
)
,
问
能
否
在
至
多
改
变
T
的
一
个
字
符
的
情
况
下
,
使
得
T
是
S
的
子
串
(
连
续
的
)
两个字符串S,都是01串), 问能否在至多改变T的一个字符的情况下,使得T是S的子串(连续的)
两个字符串S,都是01串),问能否在至多改变T的一个字符的情况下,使得T是S的子串(连续的)
题解:hash。
对
S
,
T
打
两
个
h
a
s
h
表
,
h
a
s
h
预
处
理
出
S
中
,
长
度
为
l
e
n
(
T
)
的
所
有
子
串
的
h
a
s
h
值
,
存
入
m
a
p
中
对S,T打两个hash表, hash预处理出S中,长度为len(T)的所有子串的hash值,存入map中
对S,T打两个hash表,hash预处理出S中,长度为len(T)的所有子串的hash值,存入map中
O
(
l
e
n
T
)
分
别
修
改
每
一
个
位
置
的
字
符
,
然
后
O
(
1
)
求
h
a
s
h
值
,
判
断
m
a
p
中
是
否
存
在
,
注
意
细
节
就
好
了
O(lenT)分别修改每一个位置的字符,然后O(1)求hash值,判断map中是否存在,注意细节就好了
O(lenT)分别修改每一个位置的字符,然后O(1)求hash值,判断map中是否存在,注意细节就好了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x) x&-x;
#define debugint(name,x) printf("%s: %d\n",name,x);
#define debugstring(name,x) printf("%s: %s\n",name,x);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char s[maxn],t[maxn];
ull sum[maxn];
map<ull,int>mp;
ull base = 131;
ull h1[maxn],h2[maxn],fac[maxn];
ull geth1(int l,int r){
if(l == 0) return h1[r];
return (h1[r]-h1[l-1]*fac[r-l+1]);
}
ull geth2(int l,int r){
if(l == 0) return h2[r];
return (h2[r]-h2[l-1]*fac[r-l+1]);
}
int main() {
int T;
cin>>T;
fac[0] = 1;
for(int i = 1; i < maxn; i++)
fac[i] = fac[i-1]*base;
while(T--){
mp.clear();
scanf("%s%s",s,t);
int lens = strlen(s);
int lent = strlen(t);
h1[0] = s[0];
for(int i = 1; i < lens; i++)
h1[i] = h1[i-1]*base + s[i];
h2[0] = t[0];
for(int i = 1; i < lent; i++)
h2[i] = h2[i-1]*base + t[i];
ull tmp;
for(int i = 0; i < lens-lent+1; i++){
tmp = geth1(i,i+lent-1);
mp[tmp]++;
}
if(mp[h2[lent-1]]){
puts("YES");
continue;
}
ull res;
int ok = 0;
for(int i = 0; i < lent; i++){
if(t[i] == '0') res = h2[i-1]*base+'1';
else res = h2[i-1]*base+'0';
res = res*fac[lent-1-i]+geth2(i+1,lent-1);
//cout<<res<<endl;
if(mp[res]){
ok = 1;
break;
}
}
if(ok) puts("YES");
else puts("NO");
}
}