# 1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11.

Input Specification:

Each input file contains one test case which gives a non-empty string of length no more than 1000.

Output Specification:

For each test case, simply print the maximum length in a line.

Sample Input:
Is PAT&TAP symmetric?

Sample Output:
11


#include <iostream>
#include <string>

using namespace std;

string str;

int isRevese(int s, int e){
int length = 1;
while(s < e){
if(str[s] != str[e]) return 1;
s++;
e--;
if(length == 1) length = 2;
else length += 2;
}

if(length != 1 && s == e) length++;

return length;

}

int main()
{
getline(cin,str);

int start_index = 0;
int end_index = str.length() - 1;
int max_length = -1;
int len = 0;

for(start_index = 0; start_index < str.length(); start_index++){
for(end_index = str.length() - 1; end_index >= start_index; end_index--){
len = isRevese(start_index,end_index);
if(len > max_length){
max_length = len;
}
}
}
cout << max_length << endl;
return 0;
}

#include<iostream>
#include<string.h>
#include<vector>

#define Max 10000
int dp[2*Max+1];
char str[Max];

int mmax(int a, int b)
{
if(a > b) return a;
else return b;
}
int main()
{
while(gets(str))
{
//memset(dp, -1, sizeof(dp));
int len = strlen(str);
//insert special character into str, must not appeared in str
std::vector<int> magic;
for(int i = 0; i < len; ++i)
{
magic.push_back(-1);//special character
magic.push_back(str[i]);//character to int
}
magic.push_back(-1);
//enumerate center point for magic vector
len = (int)magic.size();
int max = 1;
for(int i = 1; i < len; ++i)
{
int l, r;
int step = 1;
for(l = i-1, r = i+1; l >= 0 && r < len; l--, r++)
{
if(magic[l] != magic[r])
break;
step += 2;
}
max = mmax(max, step);
}

printf("%d\n", max/2);
}
return 0;
}

• 本文已收录于以下专栏：

## PAT 1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 ...
• u010069101
• 2014年02月27日 21:37
• 454

## 1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp...
• oFengWuYu1
• 2015年05月10日 03:17
• 1041

## PAT 1040. Longest Symmetric String (25)

http://pat.zju.edu.cn/contests/pat-a-practise/1040
• xtzmm1215
• 2014年08月22日 01:38
• 1381

## 1040. Longest Symmetric String (25)

• sunbaigui
• 2013年03月10日 21:07
• 1849

## 1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For exa...
• guoqingshuang
• 2018年02月22日 15:16
• 4

## 【C++】PAT(advanced level)1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Sta...
• artemisrj
• 2014年03月12日 10:23
• 461

## 1040. Longest Symmetric String (25)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp...
• sheng_xc
• 2013年09月26日 20:00
• 948

## A级103道PAT题目的简单总结

• jmlikun
• 2015年11月30日 21:26
• 2105

## 1040. Longest Symmetric String

Traverse the array,every times start from the current postion extend to both sides.  // 1040. Longe...
• newner
• 2013年03月13日 00:53
• 521

## 1040. Longest Symmetric String

• Staibin
• 2014年03月17日 21:26
• 530

举报原因： 您举报文章：1040. Longest Symmetric String (25) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)