当前位置:网站首页>牛客 - 鼠标的天选(字符串哈希)
牛客 - 鼠标的天选(字符串哈希)
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;
}
边栏推荐
猜你喜欢

Docker starts mysql

Charles抓包工具学习记录

PowerShell:执行 Install-Module 时,不能从 URI 下载

品牌方发行NFT时,应如何考量实用性?

The use of the database table structure document generation tool screw

DeFi明斯基时刻:压力测试与启示

实时目标检测新高地之#YOLOv7#更快更强的目标检测器

使用pipreqs导出项目所需的requirements.txt(而非整个环境)

【TPC-DS】25张表的详细介绍,SQL的查询特征
Golang协程goroutine的调度与状态变迁分析
随机推荐
【云原生--Kubernetes】Pod重启策略
Exch:重命名或删除默认邮箱数据库
并发之ReentrantLock
Golang协程goroutine的调度与状态变迁分析
推荐系统-排序层-特征工程:用户特征、物品特征
Using pipreqs export requirements needed for the project. TXT (rather than the whole environment)
Pyspark - an empty string is replaced by None
[Hello World] 二分查找笔记
redis AOF持久化个人理解
Nacos使用实践
23届微软秋招内推
审批流设计
thop 使用心得
36氪详情页AES
How does Mysql query two data tables for the same fields in two tables at the same time
并发之固定运行和交替运行方案
ArcEngine (six) use the tool tool to realize the zoom in, zoom out and translation of the pull box
VR全景市场拓展技巧之“拓客宝典”
Roson的Qt之旅#103 QML之标签导航控件TabBar
智能客服,还有多少AI泡沫?