Hdu 3068 longest retrieval Manacher Algorithm

Source: Internet
Author: User

 

This is to find the longest substring of a string, that is, the longest substring that satisfies itself.
This question seems to be possible with a suffix array and extended kmp, but it seems that the suffix array looks like it will be tle, and I learned it again.
Something special called the Manacher algorithm...
This algorithm is neither too complex nor easy to understand. Of course, it has been exposed to other string algorithms.
. I remember reading it before, but I did not understand it. I can't think of it now.
This algorithm requires additional O (N) space. It is about changing the time of space.
The general idea is to pre-process the string to make it an even string. And the first character
Yes '$'. Suppose '$' has not appeared in the original string. Add '#' before each character, and then add
'#'. For example, abc becomes $ # a # B # c #. Now we can process the new string.
Open a new array nRad [MAX]. nRad [I] indicates that the position I in the new string is extended to both the left and right sides.
And keep the maximum symmetric distance. If the nRad array is obtained, there is a conclusion that nRad [I]-1 exactly represents the original string.
The length of the corresponding position can be extended. This proof should be relatively simple, because the new string is basically the original string
Double, and each of the valid characters in the new string is inserted on both sides of #. Let's look at this example and see it.
The most important thing is how to find the nRad array.
The algorithm for finding this array mainly optimizes the nRad [I] initialization value by using some indirect conclusions. For example
When nRad [I] is used, if you know the nRad value before I and the previous position id, you can
The distance between the two sides is max. So there is a conclusion that nRad [I] can be initialized to min (nRad [2 * id-I], max-I ),
Then increment. The key is how to prove this. The proof is clear from the picture.
The proof is as follows:
When mx-I> P [j], the string centered on S [j] is contained in the string centered on S [id, because I and j are symmetric,
The substring centered on S [I] must be contained in the substring centered on S [id]. Therefore, P [I] = P [j] is required. See.


When P [j]> mx-I, the string centered on S [j] is not completely contained in the string centered on S [id, however
Symmetry, we can see that the two green boxes in the circle are the same, that is to say, the substring centered on S [I] will at least
Expand to the position of mx, that is, P [I]> = mx-I. If the parts after mx are symmetric, they can only be honestly matched.

This is very clear...

The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;

Const int MAX = 110010*2;
Char szIn [MAX];
Char szOut [MAX];
Int nRad [MAX];

Int Proc (char * pszIn, char * pszOut)
{
Int nLen = 1;

* PszOut ++ = '$ ';
While (* pszIn)
{
* PszOut ++ = '#';
NLen ++;
* PszOut ++ = * pszIn ++;
NLen ++;
}
* PszOut ++ = '#';
* PszOut = '\ 0 ';

Return nLen + 1;
}

Void Manacher (int * pnRad, char * pszStr, int nN)
{
Int nId = 0, nMax = 0;

// PnRad [0] = 1;
For (int I = 0; I <nN; ++ I)
{
If (nMax> I)
{
PnRad [I] = min (pnRad [2 * nId-I], nMax-I );
}
Else pnRad [I] = 1;

While (pszStr [I + pnRad [I] = pszStr [I-pnRad [I])
{
++ PnRad [I];
}
If (pnRad [I] + I> nMax)
{
NMax = pnRad [I] + I;
NId = I;
}
}
}

Int main ()
{
While (scanf ("% s", szIn) = 1)
{
Int nLen = Proc (szIn, szOut );
Manacher (nRad, szOut, nLen );
Int nAns = 1;
For (int I = 0; I <nLen; ++ I)
{
NAns = max (nRad [I], nAns );
}
Printf ("% d \ n", nAns-1 );
}

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.