当前位置:网站首页>牛客 - 鼠标的天选(字符串哈希)
牛客 - 鼠标的天选(字符串哈希)
2022-08-03 07:59:00 【GHOSTANDBREAD】
题目链接: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;
}
边栏推荐
猜你喜欢
随机推荐
WordPress主题-B2美化通用子主题商业运营版
如何在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster?
Roson的Qt之旅#104 QML Image控件
图解Kernel Device Tree(设备树)的使用
FusionAccess软件架构、FusionAccess必须配置的四个组件、桌面发放流程、虚拟机组类型、桌面组类型
《剑指Offer》刷题之打印从1到最大的n位数
并发之多把锁和活跃性
pyspark @udf loop using variable problem
数据监控平台
Windows安装MySQL(MIS)
二进制日志过期时间设置expire_logs_days
工控机防勒索病毒浅析
进程的创建
redis AOF持久化个人理解
并发之固定运行和交替运行方案
ArcEngine (5) use the ICommand interface to achieve zoom in and zoom out
Karatsuba大数乘法的Verilog实现
ArcEngine(四)MapControl_OnMouseDown的使用
mysql备份时的快照原理
《21天精通TypeScript-5》类型注解与原始类型









