当前位置:网站首页>字符串回文hash 模板题 O(1)判字符串是否回文
字符串回文hash 模板题 O(1)判字符串是否回文
2022-07-02 09:42:00 【蛀牙牙乐】
推荐: https://segmentfault.com/a/1190000021665249
板子题:判断回文串
双hash,单hash也能过
我实在不明白它和后面那个用数组存的有什么区别,m改这个一直wa
贴个别人过了的代码,等我补!
自然溢出翻车了,搞了个模数
单hash:
#include<iostream>
using namespace std;
#define ull unsigned long long
const int maxn = 1e6 + 5;
ull p = 13331;
const int mod = 1e9 + 7;
int main(){
int len;
while(cin >> len){
ull hash_a = 0, hash_b = 0;
getchar();
int mid = len / 2;
for (int i = 1; i <= mid; ++i){
char t = getchar();
hash_a = (hash_a * p + (ull)t) % mod;
}
if(len & 1)
getchar();
ull pp = 1;
for (int i = 1; i <= mid; ++i){
char t = getchar();
hash_b = (hash_b + (ull)t * pp) % mod;
pp = p * pp % mod;
}
if(hash_a == hash_b)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
双hash:
对两个不同的素数取模
#include<iostream>
using namespace std;
#define ull unsigned long long
const int maxn = 1e6 + 5;
ull p = 13331;
const int mod = 1e9 + 7;
const int mod1 = 1e8 + 15;
int main(){
int len;
while(cin >> len){
ull hash_a = 0, hash_b = 0,hash_aa = 0, hash_bb = 0;
getchar();
int mid = len / 2;
for (int i = 1; i <= mid; ++i){
char t = getchar();
hash_a = (hash_a * p + (ull)t) % mod;
hash_aa = (hash_aa * p + (ull)t) % mod1;
}
if(len & 1)
getchar();
ull pp = 1, pp1 = 1;
for (int i = 1; i <= mid; ++i){
char t = getchar();
hash_b = (hash_b + (ull)t * pp) % mod;
hash_bb = (hash_bb + (ull)t * pp1) % mod1;
pp = p * pp % mod;
pp1 = p * pp1 % mod1;
}
if(hash_a == hash_b && hash_aa == hash_bb)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
M形字符串
M字符串定义:一个字符串是回文,它的一半也是回文
#include<iostream>
using namespace std;
#define ull unsigned long long
const int maxn = 1e6 + 5;
ull p = 13331;
ull hash_a[maxn], hash_b[maxn], power[maxn];
void init(string s, int len){
hash_a[0] = 0, hash_b[0] = 0, power[0] = 1;
for (int i = 1; i <= len; ++i){
hash_a[i] = hash_a[i - 1] * p + (s[i] - 'a');
hash_b[i] = hash_b[i - 1] * p + (s[len - i + 1] - 'a');
power[i] = power[i - 1] * p;
}
}
bool part_judge(int l, int r, int len){
ull ta = hash_a[r] - hash_a[l - 1] * power[r - l + 1];
ull tb = hash_b[len - l + 1] - hash_b[len - r] * power[r - l + 1];
return ta == tb;
}
int main(){
string s;
cin >> s;
s = " " + s;
int len = s.size();
int ans = 0;
init(s, len);
for (int i = 1; i <= len; ++i){
if(part_judge(1, i, len) && part_judge(1, (i + 1) / 2, len))//分割成一半的时候,5 + 1 >> 2 == 3 4 + 1 >> 2 == 2不会影响结果
++ans;
}
cout << ans << endl;
return 0;
}
P3501 [POI2010]ANT-Antisymmetry
题解:https://www.cnblogs.com/henry-1202/p/10321013.html
#include<iostream>
using namespace std;
#define ull unsigned long long
#define ll long long
const int maxn = 1e6 + 5;
ull p = 13331;
ull hash_a[maxn], hash_b[maxn], power[maxn], hash_aa[maxn], hash_bb[maxn], powerr[maxn];
void init(string s, int len){
hash_a[0] = 0, hash_b[len + 1] = 0, power[0] = 1;
for (int i = 1; i <= len; ++i){
hash_a[i] = hash_a[i - 1] * p + (ull)(s[i]);
// hash_b[len - i + 1] = hash_b[len - i + 2] * p + (s[len - i + 1] == '0' ? '1' : '0');
power[i] = power[i - 1] * p;
}
for (int i = len; i ; --i){
hash_b[i] = hash_b[i + 1] * p + (ull)(s[i] == '0' ? s[i] + 1 : s[i] - 1);
}
}
ull part_judge1(int l, int r, int len){
ull ta = hash_a[r] - hash_a[l - 1] * power[r - l + 1];
return ta;
}
ull part_judge2(int l, int r, int len){
ull tb = hash_b[l] - hash_b[r + 1] * power[r - l + 1];
return tb;
}
ll judge(int st, int len){
int l = 1, r = st;
while(l <= r){
int mid = (l + r) >> 1;
if(part_judge1(st - mid + 1, st, len) == part_judge2(st + 1, st + mid, len))
l = mid + 1;
else
r = mid - 1;
}
return r;
}
int main(){
int len;
cin >> len;
string s;
cin >> s;
s = " " + s;
ll ans = 0;
init(s, len);
for (int i = 1; i < len; ++i){
ans += judge(i, len);
}
cout << ans << endl;
return 0;
}
边栏推荐
- Research on and off the Oracle chain
- Leetcode922 按奇偶排序数组 II
- [visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
- The most understandable f-string tutorial in history, collecting this one is enough
- 【C语言】十进制数转换成二进制数
- BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
- PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
- Time format display
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- How to Create a Beautiful Plots in R with Summary Statistics Labels
猜你喜欢

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator

自然语言处理系列(三)——LSTM

6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开

GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL

【2022 ACTF-wp】

K-Means Clustering Visualization in R: Step By Step Guide

GGPlot Examples Best Reference

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

YYGH-BUG-04
随机推荐
Log4j2
[untitled] how to mount a hard disk in armbian
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Jenkins用户权限管理
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
YYGH-10-微信支付
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
【2022 ACTF-wp】
[multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
Leetcode14 最长公共前缀
Easyexcel and Lombok annotations and commonly used swagger annotations
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
Fabric. JS 3 APIs to set canvas width and height
The selected cells in Excel form have the selection effect of cross shading
Leetcode209 长度最小的子数组
【2022 ACTF-wp】
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
Research on and off the Oracle chain
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)