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

L2-008. 最长对称子串

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

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

输入格式:

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

输出格式:

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

输入样例:
Is PAT&TAP symmetric?
输出样例:
11

思路:由于数据比较弱,就不考虑manacher算法啦(我不会)。。

不过可以提供一下这个算法的讲解http://www.cnblogs.com/biyeymyhjob/archive/2012/10/04/2711527.html

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     string s;
 6     getline(cin,s);
 7     int tmp,max=1;
 8     for(int i=0; i<s.size(); i++)
 9     {
10         tmp=1;
11         for(int j=1; j<=s.size(); j++)
12         {
13             if(i-j<0||i+j>s.size()) break;
14             if(s[i-j]!=s[i+j]) break;
15             tmp+=2;
16         }
17         if(tmp>max)
18             max=tmp;
19         tmp=0;
20         for(int j=1; j<=s.size(); j++)
21         {
22             if(i-j+1<0||i+j>s.size()) break;
23             if(s[i-j+1]!=s[i+j]) break;
24             tmp+=2;
25         }
26         if(tmp>max)
27             max=tmp;
28 
29     }
30     cout<<max<<endl;
31     return 0;
32 }
我会一直在
原文地址:https://www.cnblogs.com/zhien-aa/p/5659934.html