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

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

sasadasa
bxabx
zhuyuan

Sample Output

7
1
3

题意:求字符串中最长的回文串中的长度,例如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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值