Palindromic Subsequence(最長迴文字串 輸出路徑),最長迴文字串

來源:互聯網
上載者:User

Palindromic Subsequence(最長迴文字串 輸出路徑),最長迴文字串

初看好簡單   一開始調試著一直re   後來也不知道怎麼就對了  但是還有一些bug存在   ,

這道題的列印路徑和light oj An Easy LCS(ps:點擊開啟連結)一樣

但是只改一下會Tle  因為(1000*1000*1000)好大

但是把儲存的字串改為string 定義的就過了

但是還是有一點有點難受(下面會說出)

我也是醉了

#include <stdio.h>#include <string.h>#include <string>#include <iostream>using namespace std;#define maxx 1010char s1[maxx];char s2[maxx];int dp[maxx][maxx];string s[maxx][maxx];int main(){       while(scanf("%s",s1+1)!=EOF){    memset(dp,0,sizeof(dp));            int k;            int n=strlen (s1+1);            for(int i=1;i<=n;i++)                s2[i]=s1[n-i+1];            for(int i=1;i<=n;i++)                for(int j=1;j<=n;j++)            {                if(s1[i]==s2[j])                {                    dp[i][j]=dp[i-1][j-1]+1;                        s[i][j]=s1[i]+s[i-1][j-1]+s2[j];                }                  else               {                   if(dp[i-1][j]>dp[i][j-1])                   {                       dp[i][j]=dp[i-1][j];                       s[i][j]=s[i-1][j];                   }                   else if(dp[i][j-1]>dp[i-1][j])                   {                       dp[i][j]=dp[i][j-1];                       s[i][j]=s[i][j-1];                   }                   else                   {                       dp[i][j]=dp[i-1][j];                       if(s[i-1][j]>s[i][j-1])                           s[i][j]=s[i][j-1];                       else                           s[i][j]=s[i-1][j];                   }               }            }        string s3= s[n][n];//怎麼也沒有想到string定義的是這樣輸出的        int l = dp[n][n];        if(l & 1) {            for(int i=0; i<(l-1)/2; i++)                cout << s3[i];            for(int i=(l-1)/2; i>=0; i--)                cout << s3[i];        }        else {            for(int i=0; i<l/2; i++)                cout << s3[i];            for(int i=l/2-1; i>=0; i--)                cout << s3[i];        }        cout << endl;       }    }



教一個判斷迴文字串的問題,用的是C語言風格的字串,運行出錯

{
//char *str; 指標必須分配空間之後才可以用,不然野指標會造成系統嚴重問題
char str[128];
cout<<"Please input the string:"<<endl;
cin>>str;
if(Test(str))
cout<<"The string is palindromic!"<<endl;
else
cout<<"The string is not palindromic!"<<endl;
}

bool Test(char *ch)
{
//char *str1,*str2; 和主程式中的問題一樣,野指標不能直接使用
char str1[128],str2[128];
int i=0,j=0;
if(strlen(ch)==1)
return true;
else
{
for(int n=0;n<strlen(ch);++n)
{
if(isalpha(ch[n]))
str1[i++]=ch[n];
}
str1[i]=0; //串加結束符
for(int n=strlen(str1)-1;n>=0;--n) //要從最後一個字元開始,直到0位置,數組下標從0開始
{
str2[j++]=str1[n];
}
str2[j]=0; //串加結束符
if(strcmp(str1,str2))
return false; //原來反了
else
return true; //原來反了
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.