The longest symmetric substring of Pat l2-008 dynamic programming

Source: Internet
Author: User
l2-008. Longest symmetric substringTime limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Standard author Chen Yue The procedure of the sentence

For a given string, the subject requires you to output the length of the longest symmetric substring. For example, given "is Pat&tap symmetric", the longest symmetric substring is "s Pat&tap S", so you should output 11.

Input Format:

Enter a non-empty string with a length of not more than 1000 on one line.

output Format:

Prints the length of the longest symmetric substring in a row. Input Sample:

Is Pat&tap symmetric?
Output Sample:
11


This problem in the training game to see the code of others, the results of the game is still not written, only by their own thinking to stay in the depths of memory, by understanding the memory can progress quickly

But not thinking out of your mind is not going to be kept for too long in your mind without the repetitive training ....

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
using namespace Std;
#define N 100000
int p[n];
Char Str[n];

int main ()
{
Gets (str);
int Len=strlen (str);
for (int i=len;i>=0;i--)
{
Str[i+i+2]=str[i];
str[i+i+1]= ' # ';
}
str[0]= ' * ';
int id=0, maxt=0;
for (int i=2;i<2*len+1;i++)
{
if (id+p[id]>i)
{
P[id]=min (p[id+id-i],p[id]+id-i);
}
Else
{
P[i]=1;
}
while (Str[i-p[i]]==str[i+p[i]])
{
p[i]++;
}
if (I+p[i]>id+p[id])
{
Id=i;
}
if (P[I]&GT;=MAXT)
{
Maxt=p[i];
}
}
cout<<maxt-1<<endl;
return 0;
}


The following reference to the great God's way of thinking with the best solution of DP, the method is really clever

Manacher algorithm:

Defining an array P[i] represents an I-centered (including i) Palindrome string with a long radius

The maximum p[i] is the length of the longest palindrome string, and the question is how to ask for the p[i, if the ++i is to be computed from the previous for (int i=0;i<strlen (s);

Since S is previously swept, it is necessary to calculate p[i] must have been calculated p[1]....p[i-1]

Assuming the scan is now i+k This position, now you need to compute P[I+K]

The definition of MaxLen is the position at the far right end of all palindrome strings in the i+k position, that is, Maxlen=p[i]+i;//p[i]+i represents the largest

In two different situations:

1.i+k This position is not in the front of any palindrome string, that is, I+k>maxlen, then initialize p[i+k]=1;//itself is a palindrome string

Then P[i+k] left and right extension, that is while (s[i+k+p[i+k]] = = S[i+k-p[i+k]) ++p[i+k]

2.i+k This position is contained by a palindrome string preceded by the position I center, i.e. the Maxlen>i+k

In that case, P[i+k] is not starting from 1.


Because of the nature of the palindrome string, it is known that i+k this position about I and i-k symmetry,

So P[i+k] is divided into the following 3 kinds of circumstances

Black is the palindrome range of I, Blue is the i-k of the series,





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.