hdu3294(找最长回文串)

#include
#include
char str[200009];
int main()
{
    int i,hed,end,e,j,maxlen,len;
    char ch;
    while(scanf("%c %s",&ch,str)==2)
    {
        getchar();
         maxlen=0;len=strlen(str);//一定,注意这个,如果是很长的字符串不能放在循环,就必须用len,不然每一次计算长度会超时
        for(e=0;e         {
            for(i=0;e-i>=0&&2*i+1             {
                 if(str[e-i]!=str[e+i])
                    break;
                    if(2*i+1>maxlen)
                    {
                        maxlen=2*i+1;//回文串的长度
                        hed=e-i;//起始位置
                        end=e+i;//终了位置
                    }
            }

             for(i=0;e-i>=0&&2*i+2             {
                 if(str[e-i]!=str[e+i+1])
                    break;
                    if(2*i+2>maxlen)
                    {
                        maxlen=2*i+2;
                        hed=e-i;
                        end=e+i+1;
                    }
            }
        }

        if(end-hed>=2)
            {
                printf("%d %d\n",hed,end);
                for(i=hed;i<=end;i++)//这样可以省时间,每次只写出一段真正的回文字符串
                if(ch<=str[i])
                  printf("%c",str[i]-ch+'a');
                    else
                printf("%c",'z'-(ch-str[i])+1);
            }
        else
        printf("No solution!");

        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(ACM,字符串)