O (n) echo substring Algorithm
Note: I have found that the source code below is a bit of a bug... Corrected it in the next blog ..
Here, I will introduce a method for processing O (n) input strings. Manacher algorithm.
Original article address:
Http://zhuhongcheng.wordpress.com/2009/08/02/a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/
In fact, the original text is quite clear, but only in English. I 'd like to write a chinese copy here.
First, we all know what it is called a back-to-text string. This algorithm is used to solve the length of the longest back-to-text substring in a string. This algorithm can calculate the maximum number of times that each character is centered in the linear time complexity in the O (n) time complexity,
This algorithm has a clever place. It unifies the odd and even strings. These 1.1 cases are annoying in the case of text strings. This algorithm also makes full use of the particularity of character matching to avoid a large number of unnecessary repeated matching.
This is the general process of the algorithm. First, insert a separator between each two adjacent characters. Of course, this separator must not appear in the original string. Generally, they can be separated. In this way, the odd-length and even-length return strings are cleverly unified (see the following example, the length of the return string is all odd ), then, an auxiliary array P is used to record the information of the longest return string centered on each character. P [ID] records the longest return string centered on STR [ID]. When STR [ID] is the first character, the maximum length of the input string is P [ID] characters to the right.
Original string: w aa bwsw F D
New String: # W# #B# W # s # W #F # D #
Auxiliary array P: 1 2 1 2 2 2 1 2 1 2 1 1 4 1 2 2 1 2 1
Here is a good property. P [ID]-1 is the length of the substring in the original string (including '#'). If this is not particularly clear, you can take out a piece of paper to draw a picture. Of course, each person may write differently here, but I think the general idea should be the same.
Okay. Let's continue. The key issue is how to obtain the P array in the O (n) time complexity. As long as the P array is obtained, the longest string can be directly scanned.
Because this algorithm is linearly scanned from the front to the back. So when we are preparing to calculate P [I], we have obtained the previous P [J. We use MX to record the input string before I and extend it to the rightmost position. At the same time, use the ID variable to write down the id value for obtaining the optimal MX. (Note: to prevent cross-border Characters During character comparison, I added another special character '$' before the '#' string ', so my new string subscript starts from 1)
Well, here we can paste a piece of code first.
Copy code
- Void PK ()
{ Int I; Int MX = 0; Int ID; For (I = 1; I <n; I ++) { If (MX> I) P [I] = min (P [2 * ID-I], Mx-I ); Else P [I] = 1; For (; STR [I + P [I] = STR [I-P [I]; P [I] ++) ; If (P [I] + I> MX) { MX = P [I] + I; Id = I; } } }
|
Isn't the code very short? It's quite easy to write. It's very convenient. Remember that the algorithm I mentioned above avoids many unnecessary duplicate matches. What does this mean? In fact, this is a code.
If(
MX> I) P [I] =Min(
P [2 * ID-I],
Mx-I );
When MX> I is the farthest length compared to the current surface, P [I] has a minimum value. The core idea of this algorithm is here. Why does the P array satisfy such a property? (The following part is in the Image Format)
After reading this algorithm, you may think where this algorithm will be used? In fact, the suffix array of the return string can also be used. Only the complexity is O (n log
N), and generally, it does not deliberately clip a log n algorithm. This is exactly the case for HDU. You Need To Write T in an array with a suffix (of course I should have written too badly ). If you don't believe it, you can try it.
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3068
In addition, a copy of the AC code is provided.
Http://acm.hust.edu.cn: 8080/judge/problem/viewsource. Action? Id = 140283