南阳 132 最长回文子串

来源:互联网 发布:连云港seo公司 编辑:程序博客网 时间:2024/05/31 13:16
#include <stdio.h>#include <string.h>#include <ctype.h>char str[5010];int pos[5010];char ch[5010];int main(){    int i,j,k;    int T;    int len;    int max;    int from,to;    scanf("%d%*c",&T);    //getchar();    while(T--)    {        memset(str,0,sizeof(str));        memset(pos,0,sizeof(pos));        memset(ch,0,sizeof(ch));        //gets        gets(str);        len = strlen(str);        j = 0;        for(i=0; i<len; i++)        {            if(isalpha(str[i]))            {                ch[j] = toupper(str[i]);                pos[j++] = i;            }        }        len = j;        j = 0;        max = -1;        for(i=0; i<len; i++)        {            //奇数            for(j=0; i+j<len&&i-j>=0; j++)            {                if(ch[i-j]!=ch[i+j])                    break;                if(2*j+1>max)                {                    max = 2*j+1;                    from = i-j;                    to = i+j;                }                }            //偶数            for(j=0; i+j+1<len&&i-j>=0; j++)            {                if(ch[i-j]!=ch[i+j+1])                    break;                if(2*j+2>max)                {                    max = 2*j+2;                    from = i-j;                    to = i+j+1;                }            }        }        for(i=pos[from]; i<=pos[to]; i++)        {            putchar(str[i]);        }        putchar('\n');    }    return 0;}

0 0