当前位置:网站首页>[template record] string hash to judge palindrome string
[template record] string hash to judge palindrome string
2022-07-19 03:10:00 【Which bug are you?】
Zhejiang Province competition The first I topic Barbecue
The third place of Zhejiang provincial competition I topic Barbecue, Use string hash to do .
Hash the palindrome string : Calculate the past hash value of a string , Then calculate the hash value of the string in reverse . Then compare the hash value of positive and negative strings , Equality means palindrome .
Reference blog :I. Barbecue_giao Source's blog -CSDN Blog
character string hash Algorithm solves palindrome string - SegmentFault Think no
Post the following code , When you encounter the same problem in the future, use it as a template .( In fact, I wrote several hash methods and comparisons , But this kind of relatively speaking is best understood )
#include <iostream>
#include <string>
using namespace std;
typedef unsigned long long ull;
#define base 2333
const ull N = 1e6 + 5;
ull hash1[N];
ull hash2[N];
ull prev_t[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
string str;
cin >> str;
int len = str.size();
prev_t[0] = 1;
hash1[0] = 0;
hash2[0] = 0;
for (int i = 1; i <= len; i++)// The string subscript is i Corresponding to hash array i+1
{
hash1[i] = hash1[i - 1] * base + str[i - 1] - 'a' + 1;
hash2[i] = hash2[i - 1] * base + str[len - i] - 'a' + 1;// The hash value of the reverse string
prev_t[i] = prev_t[i - 1] * base;// The array works when getting the hash value of the substring
}
while (q--)
{
int l, r;
cin >> l >> r;
int str_lr = r - l + 1;
if (str_lr % 2 == 0)
{
cout << "Budada\n" ;
}
else if ((hash1[r] - hash1[l - 1] * prev_t[r - l + 1]) ==
hash2[len - l + 1] - hash2[len - r] * prev_t[r - l + 1])// Compare the
{
cout << "Budada\n";
}
else
{
cout << "Putata\n";
}
}
return 0;
}


边栏推荐
- Graphql first acquaintance
- [MCU simulation] (VI) addressing mode - index addressing and relative addressing
- Thinking for half a year
- 05_ Service call ribbon
- 需要慢一点点
- [MCU simulation] (XIX) introduction to assembly, assembly instructions, pseudo instructions
- 【单片机仿真】(八)指令系统 — 数据传送指令
- Ncnn mat matrix class
- [MCU simulation] (V) addressing mode - immediate addressing and register indirect addressing
- 2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
猜你喜欢

Summary of the most complete methods of string interception in Oracle

CorelDRAW 安装不了解决方法

Redis' simple dynamic string SDS
![[face recognition] face recognition based on histogram histogram with matlab code](/img/a6/ae776d084a207647501ca951e32000.png)
[face recognition] face recognition based on histogram histogram with matlab code

Several methods of face detection

The place where the dream begins ---- first knowing C language

yolov5 opencv DNN 推理

Polynomial interpolation fitting (I)
![2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )](/img/e4/ff7f1e19583f42377307de7291f870.png)
2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )

Graphql first acquaintance
随机推荐
[MCU simulation] (XVII) control transfer instructions - call and return instructions
Specifications, multi table query basis
我最高产的EasyPyPI又双叒叕更新了!v1.4.0发布
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
【单片机仿真】(五)寻址方式 — 立即寻址与寄存器间接寻址
【人脸识别】基于直方图Histogram实现人脸识别附matlab代码
ncnn paramdict&modelbin
仿射变换实现
Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...
Code demonstration of fcos face detection model in openvino
Graphql first acquaintance
多项式插值拟合(三)
DDD 超越 MVC了吗
数据源对象管理(第三方对象资源) & 加载properties文件
无线用的鉴权代码
First blog ----- self introduction
JPA初识(ORM思想、JPA的基本操作)
C语言基础Day4-数组
[MCU simulation] (VII) addressing mode - bit addressing