团体程序设计天梯赛-练习集 L2-008. 最长对称子串 解题报告

来源:互联网 发布:网络信息时代的特征 编辑:程序博客网 时间:2024/05/21 16:45

L2-008. 最长对称子串

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s",于是你应该输出11。

输入格式:

输入在一行中给出长度不超过1000的非空字符串。

输出格式:

在一行中输出最长对称子串的长度。

输入样例:
Is PAT&TAP symmetric?
输出样例:
11
#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <algorithm>#include <queue>using namespace std;int main(){    char s[1010];    gets(s);    int len = strlen(s);    int ans = 0;    for (int i = 0; i < len; i++)    {        for (int j = 0; i - j >= 0 && i + j < len; j++)        {            if (s[i-j] != s[i+j]) break;            if (2*j+1 > ans) ans = 2*j+1;        }        for (int j = 0; i - j >= 0 && i+j+1 < len; j++)        {            if (s[i-j] != s[i+j+1]) break;            if (2*j+2 > ans) ans = 2*j+2;        }    }    printf("%d\n", ans);    return 0;}



0 0
原创粉丝点击