当前位置:网站首页>字符串回文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;
}
边栏推荐
- QT meter custom control
- 二分刷题记录(洛谷题单)区间的甄别
- Log4j2
- 【C语言】杨辉三角,自定义三角的行数
- Implementation of address book (file version)
- [visual studio 2019] create and import cmake project
- 基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
- conda常用命令汇总
- [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
- How to Create a Nice Box and Whisker Plot in R
猜你喜欢

Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)

Natural language processing series (I) -- RNN Foundation

HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R

深入理解PyTorch中的nn.Embedding

Cluster Analysis in R Simplified and Enhanced

Seriation in R: How to Optimally Order Objects in a Data Matrice

Esp32 audio frame esp-adf add key peripheral process code tracking

The selected cells in Excel form have the selection effect of cross shading

Tiktok overseas tiktok: finalizing the final data security agreement with Biden government

R HISTOGRAM EXAMPLE QUICK REFERENCE
随机推荐
Leetcode922 按奇偶排序数组 II
Filtre de profondeur de la série svo2
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
深入理解P-R曲线、ROC与AUC
PyTorch nn. Full analysis of RNN parameters
Some problems encountered in introducing lvgl into esp32 Arduino
史上最易懂的f-string教程,收藏這一篇就够了
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
YYGH-BUG-04
CMake交叉编译
行業的分析
文件操作(详解!)
What week is a date obtained by QT
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
(C语言)八进制转换十进制
Time format display
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
to_bytes与from_bytes简单示例