当前位置:网站首页>String palindrome hash template question o (1) judge whether the string is palindrome
String palindrome hash template question o (1) judge whether the string is palindrome
2022-07-02 12:10:00 【Tooth decay music】
recommend : https://segmentfault.com/a/1190000021665249
Board question : Judging palindrome string
double hash, single hash I can live
I really don't understand the difference between it and the latter one stored in array ,m Change this all the time wa
Post a code that someone else has passed , Wait for me !
Natural overflow overturned , Made a module
single 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;
}
double hash:
Modulo two different primes
#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 Shape string
M String definition : A string is palindrome , Half of it is palindrome
#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))// When split in half ,5 + 1 >> 2 == 3 4 + 1 >> 2 == 2 It won't affect the result
++ans;
}
cout << ans << endl;
return 0;
}
P3501 [POI2010]ANT-Antisymmetry
Answer key :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;
}
边栏推荐
- Log4j2
- 【2022 ACTF-wp】
- drools执行完某个规则后终止别的规则执行
- 二分刷题记录(洛谷题单)区间的甄别
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- YYGH-9-预约下单
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- 计算二叉树的最大路径和
- Yygh-9-make an appointment to place an order
猜你喜欢

5g era, learning audio and video development, a super hot audio and video advanced development and learning classic

SVO2系列之深度濾波DepthFilter

初始JDBC 编程

Map和Set

还不会安装WSL 2?看这一篇文章就够了

conda常用命令汇总

CDA data analysis -- Introduction and use of aarrr growth model

Take you ten days to easily finish the finale of go micro services (distributed transactions)

机械臂速成小指南(七):机械臂位姿的描述方法

深入理解P-R曲线、ROC与AUC
随机推荐
深入理解P-R曲线、ROC与AUC
倍增 LCA(最近公共祖先)
YYGH-BUG-04
初始JDBC 编程
【2022 ACTF-wp】
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Log4j2
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
How to Visualize Missing Data in R using a Heatmap
The most understandable f-string tutorial in history, collecting this one is enough
基于Arduino和ESP8266的连接手机热点实验(成功)
Leetcode209 subarray with the smallest length
WSL 2 will not be installed yet? It's enough to read this article
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
全链路压测
小程序链接生成
drools决策表的简单使用
Map和Set
CDA data analysis -- common knowledge points induction of Excel data processing
drools中then部分的写法