当前位置:网站首页>牛客 - 鼠标的天选(字符串哈希)
牛客 - 鼠标的天选(字符串哈希)
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;
}
边栏推荐
- Postman will return to results generated CSV file to the local interface
- mysql5.7服务器The innodb_system data file 'ibdata1' must be writable导致无法启动服务器
- 最佳高质量字体
- 002-字段不为null
- 训练正常&异常的GAN损失函数loss变化应该是怎么样的
- mysql存生僻字奇怪问题,mysql为什么不能辨别mb4字符?
- ArcEngine (six) use the tool tool to realize the zoom in, zoom out and translation of the pull box
- 图解Kernel Device Tree(设备树)的使用
- [Kaggle combat] Prediction of the number of survivors of the Titanic (from zero to submission to Kaggle to model saving and restoration)
- 36氪详情页AES
猜你喜欢
随机推荐
“==”和equals的区别
How does Mysql query two data tables for the same fields in two tables at the same time
Charles抓包工具学习记录
timestamp
redis stream 实现消息队列
Qt5开发从入门到精通——第二篇(控件篇)
Taro框架-微信小程序-调用微信支付
mysql 8.0.12 安装配置方法并--设置修改密码
剑指offer专项突击版第18天
Taro框架-微信小程序-内嵌h5页面
【云原生--Kubernetes】kubectl命令详解
volta管理node版本
wordpress: 裁剪您的图片时发生错误
Redis的基础与django使用redis
【云原生--Kubernetes】Pod容器与镜像拉取策略
day12---接口和协议
Mysql的in和exists用法区别
ArcEngine (4) Use of MapControl_OnMouseDown
greenplum role /user 管理
xshell开启ssh端口转发,通过公网机器访问内网机器