现在的位置: 首页 > 综合 > 正文

PAT 1040. Longest Symmetric String

2018年04月25日 ⁄ 综合 ⁄ 共 529字 ⁄ 字号 评论关闭

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1040

题解:

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    char ch[1005];
    gets(ch);
    int maxx=0,n=strlen(ch);
    for(int i=0;i<n;++i)
    {
        for(int j=0;i-j>=0&&i+j<n;++j)
        {
            if(ch[i-j]!=ch[i+j])
                 break;
            if(maxx<2*j+1)
                maxx=2*j+1;
        }
        for(int j=0;i-j>=0&&i+j<n;++j)
        {
            if(ch[i-j]!=ch[i+j+1])
                break;
            if(maxx<2*j+2)
                maxx=2*j+2;
        }
    }
    printf("%d\n",maxx);
    return 0;
}

来源:http://blog.csdn.net/acm_ted/article/details/20171113

抱歉!评论已关闭.