题意:
给一个字符串集合和一个长字符串,两种操作,单点修改长字符串的某个字母,查询长字符串的[L,R]区间对应字串是否在字符串集合中出现。
思路:
将字符串集合hash之后存在set<ll>中,用树状数组保存长字符串的各个位置的hash值,用sum(x)函数计算[0,x]的hash值,((sum(r)-sum(l-1))%mod+mod)%mod*qpow(qpow(base,l-1),mod-2)%mod计算出[L,R]区间的字串hash值,进行判断。
代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll INFF=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+9;
const double eps=1e-5;
const int maxn=100005;
const int maxm=20005;
const int base=27;
int T;
int n,m;
char word[2000005];
char str[100005];
set<ll>words;
void hashword(char word[]) {
int len=strlen(word);
ll res=0;
for(int i=len-1; i>=0; i--) {
res=(res*base%mod+(word[i]-'a'+1))%mod;
}
words.insert(res);
}
ll bit[100005<<1];
ll sum(int x) {
ll ret=0;
while(x>0) {
ret=(ret+bit[x])%mod;
x-=lowbit(x);
}
return ret;
}
void add(int x,ll d,int len) {
while(x<=len) {
bit[x]=(bit[x]+d)%mod;
x+=lowbit(x);
}
}
ll qpow(ll a,ll b) {
ll res=1;
while(b) {
if(b&1) {
res=res*a%mod;
}
b>>=1;
a=a*a%mod;
}
return res;
}
void init() {
words.clear();
}
ll basew[100005];
ll strhash[maxn];
void solve() {
basew[0]=1;
for(int i=1; i<=100000; i++) {
basew[i]=basew[i-1]*base%mod;
}
sd(T);
for(int kase=1; kase<=T; kase++) {
printf("Case #%d:\n",kase);
init();
sd(n);
for(int i=0; i<n; i++) {
scanf("%s",word);
hashword(word);
}
scanf("%s",str);
int len=strlen(str);
mst(bit,0);
for(int i=0;i<len;i++){
add(i+1,(ll)(str[i]-'a'+1)*basew[i],len);
}
sd(m);
for(int i=0; i<m; i++) {
getchar();
char op=getchar();
if(op=='Q') {
int l,r;
sdd(l,r);
l++;
r++;
ll now=((sum(r)-sum(l-1))%mod+mod)%mod;
now=now*qpow(qpow(base,l-1),mod-2)%mod;
if(words.count(now)) {
printf("Yes\n");
} else {
printf("No\n");
}
} else {
int x;
char ch;
sd(x);
getchar();
ch=getchar();
x++;
add(x,(ll)-1*(str[x-1]-'a'+1)*basew[x-1]%mod,len);
str[x-1]=ch;
add(x,(ll)(str[x-1]-'a'+1)*basew[x-1]%mod,len);
}
}
}
return ;
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#else
// freopen("","r",stdin);
// freopen("","w",stdout);
#endif
solve();
return 0;
}