最長迴文子串


一般求迴文子串用的是Manacher算法,但是該算法只是簡單判斷迴文,該題目中需要去除掉空格和標點,所以,自己用了動態規劃(加剪枝,取出空號等)。

代碼如下:

//最長迴文子串  動態規劃
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cctype>  //for tolower
#define MAXSIZE 5000
char str[MAXSIZE];//="Confuciuss say:Madam,I'm Adam.";
int d[MAXSIZE];  //表示狀態,以i爲末節點的迴文長度
int begin[MAXSIZE];  //以i爲末節點的迴文序列的起始位置在哪

#define COMP(x)  ((str[x]<='z'&&str[x]>='a')||(str[x]<='Z'&&str[x]>='A'))

void printstr()  //輸入字符串
{
    int i=0;
    char sh;
    printf("請輸入樣例:");
    scanf("%c",&sh);
    while(sh!='\n')
    {
        str[i++]=sh;
        scanf("%c",&sh);
    }
    str[i]=0;
}
int main()
{
    freopen("a.txt","r",stdin);
    printstr();
    int len=strlen(str);
    int i,j,k,num=1,flag=0;
    d[0]=COMP(0)?1:-1;
    begin[0]=d[0]?0:-1;
    for(i=1;i<len;i++)
    {
        if(!COMP(i))
        {
            d[i]=-1;
            begin[i]=-1;
            continue;
        }
        //給一個默認的初始化
        d[i]=1;
        begin[i]=i;
        j=i-1;
        while(d[j]==-1&&j>=0)
            j--;               //循環之後,確定j指定的位置一定是字母或者爲-1
        if(j<0)
            continue;
        if(d[j]==1)
        {
            //case1:  a  a
            if(tolower(str[j])==tolower(str[i]))
            {
                d[i]=d[j]+1;
                begin[i]=j;
            }
            //  case2:  ac   a
            else if(j-1>=0)
            {
                k=j-1;
                while(d[k]==-1&&k>=0)
                    k--;
                if(k>=0&&tolower(str[k])==tolower(str[i]))
                {
                    d[i]=d[j]+2;
                    begin[i]=k;
                }
            }
        }
        else
        {
            //case3:  sssss  s
            char same=tolower(str[begin[j]]);
            for(k=begin[j]+1;k<=j;k++)
            {
                if(COMP(k))
                {
                    if(tolower(str[k])!=same)
                        break;
                }
            }
            if(k==j+1&&tolower(str[i])==same)
            {
                d[i]=d[j]+1;
                begin[i]=begin[j];
            }
            //case4:      sdad  s
            else
            {
                int temp=begin[j]-1;
                while(d[temp]==-1&&temp>=0)
                    temp--;
                if(temp<0) ;
                else if(tolower(str[temp])==tolower(str[i]))
                {
                    d[i]=d[j]+2;
                    begin[i]=temp;
                }
            }
        }
        if(d[i]>num)
        {
            num=d[i];
            flag=begin[i];
        }
        else if(d[i]==num&&begin[i]<flag)
            flag=begin[i];
    }
    printf("請輸出樣例:");
    for(i=flag,k=1;k<=num;i++)
    {
        if(COMP(i))
            k++;
        printf("%c",str[i]);
    }
    //printf("%d\n",num);
    return 0;
}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章