-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobinCarp_stringMatching_hashing.cpp
More file actions
144 lines (128 loc) · 3.39 KB
/
robinCarp_stringMatching_hashing.cpp
File metadata and controls
144 lines (128 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll base2=1997293877,mod2=2117566807,base1=1949313259,mod1=2091573227;
int maxx = 1000005;
/////natural hashing
//ll hashing(string s,ll b,ll m){
// ll ret = 0,power=1;
// for(int i=0;i<s.size();i++){
// ret*=power;
// ret+=str[i];
// ret%=m;
// power=(power*b)%m;
// }
// return ret;
//}
//
///// double hashing
//pair<ll,ll>doubleHash(string s){
// return {hasing(s,b1,m1),hashing(s,b2,m2)};
//}
//
//ll doubleHash(string s){
// ll ret = hashing(str,b1,m1);
// ret<<=32;
// ret|=hashing(s,b2,m2);
// return ret;
//}
///single hashing
//struct Hash{
// int len;
// ll base,mod;
// vector<ll>cumulativeHash,power;
//
// Hash(){}
// Hash(string s,ll b,ll m){
// len = s.size(),mod=m,base=b;
// cumulativeHash.resize(len+3,0);
// power.resize(len+3,1);
//
// for(int i=1;i<=len;i++)
// power[i]=(power[i-1]*base)%mod;
// for(int i=1;i<=len;i++){
// cumulativeHash[i]=(cumulativeHash[i-1]*base)%mod;
// cumulativeHash[i]=(cumulativeHash[i]+s[i-1])%mod;
// }
// }
// ll rangeHash(int l,int r){
// ll ret = ((ll)cumulativeHash[l]*(ll)power[r-l+1])%mod;
// ret = (cumulativeHash[r+1]-ret)%mod;
//
// if(ret<0) ret+=mod;
//
// return ret;
// }
//};
//
//int countMatch(string text,string pattern){
// int cnt=0;
// Hash textHash = Hash(text,base1,mod1);
// Hash patternHash = Hash(pattern,base1,mod1);
//
// ll matchValue = patternHash.rangeHash(0,pattern.size()-1);
// int patternSize = pattern.size();
//
// for(int i=0;i<(text.size()-patternSize+1);i++)
// if(textHash.rangeHash(i,i+patternSize-1)==matchValue)
// cnt++;
//
// return cnt;
//}
///double Hashing
struct Hash{
int len;
ll base,mod;
vector<int>cumulativeHash,power;
Hash(){}
Hash(string s,ll b,ll m){
len = s.size(),mod=m,base=b;
cumulativeHash.resize(len+3,0);
power.resize(len+3,1);
for(int i=1;i<=len;i++)
power[i]=(power[i-1]*base)%mod;
for(int i=1;i<=len;i++){
cumulativeHash[i]=(cumulativeHash[i-1]*base)%mod;
cumulativeHash[i]=(cumulativeHash[i]+s[i-1])%mod;
}
}
inline int rangeHash(int l,int r){
int ret = ((ll)cumulativeHash[l]*(ll)power[r-l+1])%mod;
ret = (cumulativeHash[r+1]-ret)%mod;
if(ret<0) ret+=mod;
return ret;
}
};
struct stringHash{
Hash sh1, sh2;
stringHash(){}
stringHash(string s){
sh1 = Hash(s, base1, mod1);
sh2 = Hash(s, base2, mod2);
}
inline ll rangeHash(int l, int r){
return ((ll)sh1.rangeHash(l, r) << 32) ^ sh2.rangeHash(l, r);
}
};
int countMatch(string text,string pattern){
int cnt=0;
stringHash textHash = stringHash(text);
stringHash patternHash = stringHash(pattern);
ll matchValue = patternHash.rangeHash(0,pattern.size()-1);
int patternSize = pattern.size();
for(int i=0;i<(text.size()-patternSize+1);i++)
if(textHash.rangeHash(i,i+patternSize-1)==matchValue)
cnt++;
return cnt;
}
int main(){
int test,cs=1;
scanf("%d",&test);
while(test--){
string text,pattern;
cin>>text>>pattern;
int ans = countMatch(text,pattern);
printf("Case %d: %d\n",cs++,ans);
}
return 0;
}