PAT 1040. Longest Symmetric String

题目: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

你可能感兴趣的:(PAT 1040. Longest Symmetric String)