题目链接:https://ac.nowcoder.com/acm/contest/38305/F
思路:
简单的字符串匹配问题,可以用 KMP,这里用的是字符串哈希
代码:
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef unsigned long long ull;
const int P = 131, N = 1e4 + 10;
ull h[N], p[N];
int res;
ull get(int l, int r) {
return h[r] - h[l - 1] * p[r - l + 1];
}
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
int n;
cin >> n;
ull s1h = 0;
string s1 = "month";
for(int i = 1; i <= 5; i ++) s1h = s1h * P + s1[i - 1];
while(n --) {
string s;
cin >> s;
if(s.size() < 5) continue;
memset(h, 0, sizeof h);
memset(p, 0, sizeof p);
p[0] = 1;
for(int i = 1; i <= s.size(); i ++) {
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + s[i - 1];
}
for(int i = 1; i <= s.size() - 5 + 1; i ++) {
if(get(i, i + 5 - 1) == s1h) {
res ++;
break;
}
}
}
cout << res;
return 0;
}