L2-008 最长对称子串 (25 分)
摘要
Title: L2-008 最长对称子串 (25 分)
Tag: 最长对称子串
Memory Limit: 64 MB
Time Limit: 1000 ms
Powered by:NEFU AB-IN
L2-008 最长对称子串 (25 分)
-
题意
给定一个字符串,请你求出其中的最长回文子串的长度。
例如,给定字符串 Is PAT&TAP symmetric?,最长回文子串为 s PAT&TAP s,其长度是 11。 -
思路
这里是求子串,而不是子序列!
所以我们可用枚举- 串长为奇数
- 串长为偶数
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41/*
* @Author: NEFU AB-IN
* @Date: 2022-04-20 18:04:25
* @FilePath: \ACM\GPLT\L2-008_1.CPP
* @LastEditTime: 2022-04-20 19:24:01
*/
using namespace std;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
signed main()
{
IOS;
string s;
getline(cin, s);
int ans = 1;
for (int i = 0; i < SZ(s); ++i)
{
// odd
int l = i - 1, r = i + 1;
while (l >= 0 && r < SZ(s) && s[l] == s[r])
l--, r++;
ans = max(ans, r - l - 1);
// even
l = i, r = i + 1;
while (l >= 0 && r < SZ(s) && s[l] == s[r])
l--, r++;
ans = max(ans, r - l - 1);
}
cout << ans;
return 0;
}