uva 11404 Palindromic Subsequence lcs 路径输出

  Palindromic Subsequence 

A Subsequence is a sequence obtained by deleting zero or more characters in a string. A Palindrome is a string which when read from left to right, reads same as when read from right to left. Given a string, find the longest palindromic subsequence. If there are many answers to it, print the one that comes lexicographically earliest.


Constraints

  • Maximum length of string is 1000.
  • Each string has characters `a' to `z' only.

Input 

Input consists of several strings, each in a separate line. Input is terminated by EOF.

Output 

For each line in the input, print the output in a single line.

Sample Input 

aabbaabb
computer
abzla
samhita

Sample Output 

aabbaa
c
aba
aha

 

求最长回文子串

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int MAXN=1111;
int dp[MAXN][MAXN];
char str1[MAXN],str2[MAXN];
string ans[MAXN][MAXN];
int main()
{
    int l,i,j;
    while(scanf("%s",str1+1)!=EOF)
    {
        l=strlen(str1+1);
        for(i=1;i<=l;i++)
        {
            str2[l+1-i]=str1[i];
        }
        memset(dp,0,sizeof(dp));
        for(i=1;i<=l;i++)
        {
            for(j=1;j<=l;j++)
            {
                if(str1[i]==str2[j])
                {
                    ans[i][j]=ans[i-1][j-1]+str1[i];
                    dp[i][j]=dp[i-1][j-1]+1;
                }
                else
                {
                    if(dp[i-1][j]>dp[i][j-1])
                    {
                        dp[i][j]=dp[i-1][j];
                        ans[i][j]=ans[i-1][j];
                    }
                    else if(dp[i-1][j]<dp[i][j-1])
                    {
                        dp[i][j]=dp[i][j-1];
                        ans[i][j]=ans[i][j-1];
                    }
                    else
                    {
                        dp[i][j]=dp[i-1][j];
                        if(ans[i-1][j]>=ans[i][j-1])
                        {
                            ans[i][j]=ans[i][j-1];
                        }
                        else
                        {
                            ans[i][j]=ans[i-1][j];
                        }
                    }
                }
            }
        }
        string s3=ans[l][l];
       // cout<<s3<<endl;
        int n=dp[l][l];
        if(n%2)
        {
            for(i=0;i<(n-1)/2;i++)
                cout<<s3[i];
            for(i=(n-1)/2;i>=0;i--)
                cout<<s3[i];
        }
        else
        {
            for(i=0;i<n/2;i++)
                cout<<s3[i];
            for(i=n/2-1;i>=0;i--)
                cout<<s3[i];
        }
        cout<<endl;
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值