UVA - 11404

题意:求任意删除字符后所形成的最长回文,并输出字典序最小的方案
把原串反转求LIS,因为转移时不断求字典序最小导致后半部分可能并非回文,所以要前半部分输出两边
话说这方案保存可真暴力

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e3+11;
const double eps = 1e-10;
typedef long long ll;
const int oo = 0x3f3f3f3f;
ll read(){
    ll x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char str[maxn],strr[maxn];
int dp[maxn][maxn];
string ans[maxn][maxn];
int main(){
    while(~s1(str)){
        int n=strlen(str+1);
        rep(i,1,n) strr[n-i+1]=str[i];
        memset(dp,0,sizeof dp);
        rep(i,0,n)rep(j,0,n) ans[i][j]=" ";
        int len=0;
        rep(i,1,n) rep(j,1,n){
            if(str[i]==strr[j]){
                dp[i][j]=dp[i-1][j-1]+1;
                ans[i][j]=ans[i-1][j-1]+str[i];
            }else{
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(dp[i-1][j]>dp[i][j-1]) ans[i][j]=ans[i-1][j];
                else if(dp[i][j-1]>dp[i-1][j]) ans[i][j]=ans[i][j-1];
                else if(ans[i][j-1]<ans[i-1][j]) ans[i][j]=ans[i][j-1];
                else ans[i][j]=ans[i-1][j];
                
            }
        }
        if(dp[n][n]&1){
            rep(i,1,(dp[n][n]+1)/2) cout<<ans[n][n][i];
            rep(i,1,dp[n][n]/2) cout<<ans[n][n][dp[n][n]/2-i+1];
            cout<<endl;
        }else{
            rep(i,1,dp[n][n]/2) cout<<ans[n][n][i];
            rep(i,1,dp[n][n]/2) cout<<ans[n][n][dp[n][n]/2-i+1];
            cout<<endl;
        }
        // cout<<ans[n][n]<<endl;
    }
    return 0;
}
posted @ 2018-03-18 14:48  Caturra  阅读(210)  评论(0编辑  收藏  举报