当前位置:网站首页>[26. String hash]
[26. String hash]
2022-07-25 01:03:00 【Little silly bird_ coding】
summary
- Where strings are used, you can generally use KMP Algorithm . use KMP Algorithms can generally use string hashing . The code is simpler .( Special hash method , String prefix hashing )
- Turn a string into a p Hexadecimal Numbers ( Hash value ), Mapping different strings to different numbers .(
Comparing whether the prefixes of two interval strings are equal becomes comparing whether the hashes of two interval strings are the same)
The core
With a K Hexadecimal angle , Let's treat strings as numbers .
Ideas
- Hash the prefix of each string ( Preprocess prefix hash )
- Hash the interval string , Use the formula to calculate the hash value of the string interval
- Calculate whether the two interval strings are equal, and convert it into whether the hash values of the two strings are equal
Two questions :
problem 1: How do we define the hash value of a prefix
- To form such as X1X2X3⋯Xn - 1Xn String , In characters ascii Code times P To the power of .
- Mapping formula :(X1×Pn- 1+X2×Pn- 2+⋯+Xn -1×P1+Xn×P0) mod Q
character string str = "ABCAB"
character string str The prefix is AABABCABCA hypothesis A~Z Map to subscript 1 ~ 26.
h[0] = 0
h[1] = "A" Of hash value
h[2] = "AB" Of hash value
h[3] = "ABC" Of hsah value
h[4] = "ABCA" Of hash value
Prefix ABCA Hash value of = (1 2 3 1 )p = (1 * p3 + 2 * p2 + 3 * p1 + 1 * p0)
step
- Think of strings as p Binary number , String has 10 Letters , As 10 digit
- hold p Convert a decimal number to a decimal number
- Put the calculated number on a smaller number Q( Put any string , Map to from 0 The natural number of the beginning )
matters needing attention :
- Any character cannot be mapped to 0, Otherwise, different strings will be mapped to 0 The situation of , such as A,AA,AAA All are 0
- The question of conflict : Through clever settings P (131 or 13331) , Q (264) Value , There is little conflict at this time .
problem 2: What is the use of prefix hashes
- You can use prefix hash to calculate the hash of any substring through a formula
The problem is to compare whether the substrings of different intervals are the same , Whether the corresponding hash value is the same .
- Finding the hash value of a string is equivalent to finding the prefix and , Finding the hash value of the substring of a string is equivalent to finding the partial sum .
Prefixes and formulas : h[ i + 1]=h[i] × P + str[i] i∈[0,n−1]i∈[0,n−1] h Prefix and array ,str For string array
Interval and formula :h[l,r]=h[r]−h[l−1]×Pr−l+1
Understanding of intervals and formulas : ABCDE And ABC The first three character values of are the same , Just two ,
Multiply P2P2 hold ABC Turn into ABC00, Reuse ABCDE - ABC00 obtain DE Hash value of .
subject
Given a length of n String , Re given m A asked , Each query contains four integers l1,r1,l2,r2, Please judge [l1,r1][l1,r1] and [l2,r2][l2,r2] Whether the strings and substrings contained in these two intervals are exactly the same .
The string contains only uppercase and lowercase English letters and numbers .
Input format
The first line contains integers n and m, Represents the length of the string and the number of queries .
The second line contains a length of n String , The string contains only uppercase and lowercase English letters and numbers .
Next m That's ok , Each line contains four integers l1,r1,l2,r2, Indicates the two intervals involved in a query .
Be careful , The position of the string is from 1 Numbered starting .
Output format
Output a result for each query , If two string substrings are identical, output
Yes, Otherwise outputNo.Each result takes up one line .
Data range
1≤n,m≤105
sample input :
8 3 aabbaabb 1 3 5 7 1 3 6 8 1 2 1 2sample output :
Yes No Yes
Code
#include <iostream>
using namespace std;
using ULL = unsigned long long;
//typedef unsigned long long ULL;
const int N = 100010, P = 131;
int n, m;
char str[N];
ULL h[N], p[N]; //p[] Mainly several powers
// h[i] front i A character hash value
// The string becomes a p Hexadecimal Numbers , Embodies the character + The order , You need to make sure that different strings correspond to different numbers
// P = 131 or 13331 Q=2^64, stay 99% There will be no conflict
// Use scenarios : Whether the substrings of two strings are the same
ULL get(int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
scanf("%d%d%s", &n, &m, str + 1);
p[0] = 1; // String from 1 Numbered starting ,h[1] Is the hash value of the previous character
h[0] = 0;
for (int i = 1; i <= n; i ++)
{
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + str[i]; // Prefix and hash the entire string
}
while (m --)
{
int l1, r1, l2, r2;
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
if (get(l1, r1) == get(l2, r2) )puts("Yes");
else puts("No");
}
return 0;
}
边栏推荐
- 如何创建索引
- A string "0" was actually the culprit of the collapse of station b
- Several states of the process
- 7.20 - daily question - 408
- How to empty localstorage before closing a page
- Example analysis of recombinant monoclonal antibody prosci CD154 antibody
- Worthington cytochrome c digestion study carboxypeptidase B scheme
- 7.19 - daily question - 408
- Method properties of ASP adodb.stream object
- 7.18 - daily question - 408
猜你喜欢

Summary of MATLAB basic grammar

On let variable promotion

Example analysis of recombinant monoclonal antibody prosci CD154 antibody
![Detailed explanation of zero length array in C language (1) [information at the end of the article]](/img/89/1f01e24ce52b2d459f26397cd8527f.png)
Detailed explanation of zero length array in C language (1) [information at the end of the article]

Advanced multithreading (Part 2)

WPF implements RichTextBox keyword query highlighting

How to implement a state machine?

Worthington carboxyl transfer carbonic anhydrase application and literature reference
![Nacos hand to hand teaching [i] dynamic configuration of Nacos](/img/c4/ae29475c795e879683227de5ba3cfc.png)
Nacos hand to hand teaching [i] dynamic configuration of Nacos

C recursively obtains all files under the folder and binds them to the treeview control
随机推荐
Unity3d calls between different script functions or parameters
Detailed explanation of alexnet of paddlepaddle paper series (with source code)
[development tutorial 10] crazy shell · open source Bluetooth smart health watch OTA image production and download technical documents
Worthington carboxyl transfer carbonic anhydrase application and literature reference
Example analysis of enum data type in MySQL
Password input box and coupon and custom soft keyboard
Heavy forecast! Analysys, together with Microsoft and the Central University of Finance and economics, talks about the digital economy
Wireshark packet capturing and rapid packet location skills
Codeworks round 650 (Div. 3) ABCD solution
Nacos hand to hand teaching [i] dynamic configuration of Nacos
Number of palindromes in question 5 of C language deduction (two methods)
494. Target sum · depth first search · knapsack problem
Dpdk based basic knowledge sorting-01
Free personal virtual machine - AWS free EC2 package
Volley7 – networkdispatcher obtains data from the network [easy to understand]
Wireshark introduction and packet capturing principle and process
Which automation tools can double the operation efficiency of e-commerce?
[FAQ of waiting insurance] can the waiting insurance evaluation organization help with the waiting insurance rectification?
Unity image control and rawimage
What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?