1040. Longest Symmetric String (25)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
gets(char * s)读入一行
max初始为1
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <memory.h>
- #include <cstdio>
- #include <cstdlib>
- #include <vector>
- using namespace std;
- #define MAX 0Xfffffff
- char s[1001];
- int main(){
- //freopen("in.txt", "r", stdin);
- gets(s);
- int len = strlen(s);
- int max = 1;
- for(int i=0;i<len;++i){
- if(i+1<len && s[i]==s[i+1]){
- for(int j=1;j<len;++j){
- if(i-j>=0 && i+1+j<len){
- if(s[i-j]!=s[i+1+j]){
- break;
- }
- if(max<2*j+2)
- max = 2*j+2;
- }else
- break;
- }
- }
- for(int j=1;j<len;++j){
- if(i-j>=0 && i+j<len){
- if(s[i-j]!=s[i+j]){
- break;
- }
- if(max<2*j+1)
- max = 2*j+1;
- }else
- break;
- }
- }
- cout<<max<<endl;
- //fclose(stdin);
- }