2017-金马五校程序设计竞赛-E -Find Palindrome

来源:互联网 发布:itunes无法连接到网络 编辑:程序博客网 时间:2024/05/17 01:01

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 ≤ |S | ≤ 50).

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

Sample Input

sasadasabxabxzhuyuan

Sample Output

713

题意:求字符串中最长的回文串中的长度,例如sasadasa
中asadasa 是最长的回文串,,

#include<bits/stdc++.h>using namespace std;string expand_palindrome(string s, int l, int r) {    int n = s.length();    while(l >= 0 && r < n && s[l] == s[r]) {        l--;        r++;    }    return s.substr(l + 1, r - l - 1);}//函数解释详见下面链接string get_longest_palin(string s) {    int i;    int n = s.length();    string longest = s.substr(0, 1);    string t;    for(i = 0; i < n; i++) {        t = expand_palindrome(s, i, i);        if(t.length() > longest.length()) {            longest = t;        }        t = expand_palindrome(s, i, i + 1);        if(t.length() > longest.length()) {            longest = t;        }    }    return longest;}int main(){    string s;    while(cin>>s)    {        cout<<get_longest_palin(s).size()<<endl;    }    return 0;}

http://blog.csdn.net/zzran/article/details/8517653

阅读全文
0 0
原创粉丝点击