Find Palindrome(dp 金马五校赛-东华大学)

来源:互联网 发布:seo关键字是什么 编辑:程序博客网 时间:2024/05/02 20:56

Problem E : Find Palindrome


From: DHUOJ, 2017060305

<a type="button" class="btn btn-default" href="/solution/submit.html?problemId=5288">Submit</a> (Out of Contest)

Time Limit: 1 s

Description

Given a string S, which consists of lowercase characters, you need to find the longest palindromic sub-string.

A sub-string of a string S  is another string S'  that occurs "in"S. For example, "abst" is a sub-string of "abaabsta". A palindrome is a sequence of characters which reads the same backward as forward.

 

Input

There are several test cases.
Each test case consists of one line with a single string S (1 ≤ || ≤ 50).

 

Output

For each test case, output the length of the longest palindromic sub-string.

 

Sample Input

sasadasabxabxzhuyuan

 

Sample Output

713

 


Author: Kenny


利用dp来记录当前位置的最长回文串长度


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std; #define MIN(a,b) ((a) < (b) ? (a) : (b))  int maxid;        // 最长回文子串下标int LPS_rb[100];  // i为中心的回文子串右边界下标right borderchar str[100];    // 原字符串处理后的副本int maxlen;void LPS_linear(char * X, int xlen){    maxlen = maxid = 0;      str[0] = '$';  // 将原串处理成所需的形式    char *p = str;    *(++p)++ = '#';    while((*p++ = *X++) != '\0')    {        *p++ = '#';    }      for(int i = 1; str[i]; ++i)  // 计算LPS_rb的值    {        if(maxlen > i)          // 初始化LPS[i]        {            LPS_rb[i] = MIN(LPS_rb[2*maxid-i],(maxlen-i));        }else        {            LPS_rb[i] = 1;        }        while(str[i-LPS_rb[i]] == str[i+LPS_rb[i]]) // 扩展        {            ++LPS_rb[i];        }        if(LPS_rb[i]-1 > maxlen)        {            maxlen = LPS_rb[i]-1;            maxid = i;        }    }} int main(){    char s[51];    while(cin>>s){        LPS_linear(s,strlen(s));        printf("%d\n", maxlen);    }    return 0;}



阅读全文
0 0