"蔚来杯"2022牛客暑期多校训练营9 G Magic Spells【马拉车+哈希】
四川今天又上热搜了,继南部疫情的未雨绸缪后,龙槽沟是真的倾盆大雨了。我没有兴趣虚伪矫情地对罹难的游人表达同情,因为人与人互不相通徒增谈资;我也没有兴趣居高临下地对擅闯的愚人表达不屑,因为你我皆为乌合之众,在流媒体的灯红酒绿下娱乐至死。地球上最聪明的大脑像条二极管,只能接受最简单的二元判断,稍微复杂的变量就足以使之熔断。基础工作人员与群众大抵是存在着些割裂的,只要就着喇叭喊叫,就一定是在表达“认同你”或“否定你”其一,绝无“陈述事实”一说。学了几十年逻辑,还是顺应情绪,不断窥视大众行为决断,也是,法不责众嘛。有人扭曲身形挤进人群刷安全感,有人刻意和大众对立谋求优越感,殊途原来也是可以同归的呵。
亚当夏娃唉声叹气
达尔文骂骂咧咧地投靠上帝
hash求子串可以O(1),
$click$ $for$ $codes$
# include "bits/stdc++.h"
using namespace std;
constexpr int N = 3e5 + 3;
char str[N], str_new[N << 1];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
unordered_map<unsigned long long, int> mp;
unordered_map<unsigned long long, int> mp2;
unordered_map<unsigned long long, bool> vis[6];
unordered_map<unsigned long long, bool> vis2[6];
int ans = 0;
str_new[0] = '!';
str_new[1] = '@';
for(int i = 1; i <= n; ++i) {
cin >> str + 1;
int len = strlen(str + 1);
int len_new = 1;
for(int j = 1; j <= len; ++j) {
str_new[++len_new] = str[j];
str_new[++len_new] = '@';
}
str_new[++len_new] = '#';
int r = 0, center;
vector<int> p(len_new + 1 << 1);
vector<int> pos;
for(int j = 1; j <= len_new; ++j) {
p[j] = (j < r) ? min(p[(center << 1) - j], r - j) : 1;
while(str_new[j - p[j]] == str_new[j + p[j]]) ++p[j];
if(r < j + p[j]) {
r = j + p[j];
center = j;
}
if(p[j] > 1) pos.push_back(j);
}
constexpr unsigned long long base = 122777, mod = 91815541, mod2 = 19260817; // hash value, which is indescibable
vector<unsigned long long> h(len + 1), b(len + 1), h2(len + 1), b2(len + 1);
b[0] = 1, b2[0] = 1;
for(int i = 1; i <= len; ++i) {
h[i] = (h[i - 1] * base % mod + (unsigned long long) str[i]) % mod;
h2[i] = (h2[i - 1] * base % mod2 + (unsigned long long) str[i]) % mod2;
b[i] = b[i - 1] * base % mod;
b2[i] = b2[i - 1] * base % mod2;
}
auto hash = [&](int l, int r) -> unsigned long long {
return (h[r] - h[l - 1] * (unsigned long long)b[r - l + 1] % mod + mod) % mod;
};
auto hash2 = [&](int l, int r) -> unsigned long long { // double hash reduces chance and thus improves accuracy
return (h2[r] - h2[l - 1] * (unsigned long long)b2[r - l + 1] % mod2 + mod2) % mod2;
};
for(auto j : pos) {
int len = p[j];
while(len) {
if((j - len + 2 >> 1) > (j + len - 2 >> 1)) break; // from several manual simulations, we'll find that the palindrome string centered on i in the new string is (i - p[i】 + 2) / 2 ---> (i + p[i] - 2) / 2 in the corresponding position in the original string
unsigned long long sub_str_1 = hash( (j - len + 2 >> 1), (j + len - 2 >> 1) );
unsigned long long sub_str_2 = hash2( (j - len + 2 >> 1), (j + len - 2 >> 1) );
if(vis[i][sub_str_1] == true && vis2[i][sub_str_2] == true) break;
vis[i][sub_str_1] = vis2[i][sub_str_2] = true;
++mp[sub_str_1], ++mp2[sub_str_2];
if(mp[sub_str_1] >= n && mp2[sub_str_2] >= n) ++ ans;
len -= 2; // manachar only directly finds the longest palindrome string, but the string may contain other substrings
if(len < 0) break;
}
}
}
cout << ans;
return 0;
}
/*
9999
abcba
abcccba
*/

"蔚来杯"2022牛客暑期多校训练营9 G Magic Spells【马拉车+哈希】的更多相关文章
- 蔚来杯2022牛客暑期多校训练营5 ABCDFGHK
比赛链接 A 题解 知识点:图论,dp. 暴力建图,连接所有点的双向通路,除了原点是单向的,并且把路径长度作为权值. 随后,从原点出发(\(f[0] = 0\),其他点负无穷,保证从原点出发),按照权 ...
- 蔚来杯2022牛客暑期多校训练营6 ABGJM
比赛链接 A 题解 知识点:数学,构造. 题目要求构造一个长为 \(m\) 的序列 \(c\) ,\(m\) 自选,使得 \(c\) 的无限循环序列 \(b\) 中任意连续 \(a_i\) 个数中都存 ...
- 蔚来杯2022牛客暑期多校训练营7 CFGJ
比赛链接 C 题解 方法一 知识点:思维. 先统计没有出现的数,每个都可以随便放,所以作为补位用的. 将原数组左移一位作为预定的答案数组,然后开始检查.如果和原数组一样,则用补位数字填充,如果不一样就 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- 2021牛客暑期多校训练营3 J 思维
传送门 J-Counting Triangles_2021牛客暑期多校训练营3 (nowcoder.com) 题目 Goodeat finds an undirected complete graph ...
- B-xor_2019牛客暑期多校训练营(第四场)
题意 给出n个数组(每组数个数不定),m个询问 l, r, x 序号在区间\([l,r]\)的每个数组是否都可以取出任意个数异或出x 题解 判断一个数组能否异或出x,是简单的线性基问题 判断多个线性基 ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
随机推荐
- mac安装java环境
1.java安装包获取: 链接:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html 2.验证安装是否成 ...
- CabloyJS部署了一套演示站点
为了方便大家快速体验和了解CabloyJS的风格和特性,全新部署了一套演示站点.对于初次接触CabloyJS的开发者,不用下载新建项目,就可以直接体验CabloyJS了 在线演示 场景 链接/二维码 ...
- Docker容器Nginx负载均衡配置、check及stub模块安装
Nginx是一款高性能的HTTP和反向代理.负载均衡web服务器.本次在Docker容器中部署三个tomcat,Nginx代理三个tomcat服务(以下称节点)来模拟实现负载均衡效果,配置check模 ...
- .NET中的 Count()、Count、Length 有什么不同
更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年7月15日. Count().Count.Length,都用于获得序列长度或者说元素的个数,但它们有什么明确的区 ...
- SpringBoot之:SpringBoot中使用HATEOAS
目录 简介 我们的目标 构建Entity和Repository 构建HATEOAS相关的RepresentationModel 构建Controller HATEOAS的意义 总结 简介 HATEOA ...
- 2 Elment Ui 日期选择器 格式化问题
Elment Ui 日期选择器 格式化问题 在前后端联调过程中 我们常常会遇到日期无法被反序列化 这个问题 会有点头疼 下面以我这边为例 后端使用的是springboot 默认集成了jackjson ...
- C语言学习之我见-strcmp()字符串比较函数
strcmp()函数,用于两个字符串的比较. (1)函数原型 int strcmp(const char *_Str1,const char *_Str2); (2)头文件 string.h (3)功 ...
- TypeScript(6)函数
函数 函数是 JavaScript 应用程序的基础,它帮助你实现抽象层,模拟类,信息隐藏和模块.在 TypeScript 里,虽然已经支持类,命名空间和模块,但函数仍然是主要的定义行为的地方.Type ...
- SAP Office Excel Intergration
*&---------------------------------------------------------------------* *& Report DEMOEXCEL ...
- Spring jdbctemplate和事务管理器
内部bean 对象结构: @Autowiredprivate IAccountService accountService; @Service("accountService")@ ...