PAT甲级 1040

来源:互联网 发布:装修软件哪个好 编辑:程序博客网 时间:2024/05/29 19:05

Longest Symmetric String

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


  1. 注意一个奇对称和一个偶对称。
#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxn=1000+1;char input[maxn];bool pointInInterval(int pos){    if(pos>=0 && pos<strlen(input))return true;    return false;}int getLengthByCurPos(int l,int r){    while(pointInInterval(l) && pointInInterval(r) && input[l]==input[r]){        --l,++r;    }    return r-l-1;}int main(){    gets(input);    int rst=0;    for(int i=0;i<strlen(input);i++){        rst=max(rst,getLengthByCurPos(i,i));        rst=max(rst,getLengthByCurPos(i,i+1));    }    printf("%d",rst);}
原创粉丝点击