UVA 11404 palindromic subsequence[dp LCS Print]

Source: Internet
Author: User

Uva-11404palindromic subsequence

Test instructions: A string that deletes 0 or more characters and outputs the smallest and longest palindrome string in the dictionary order

does not require path interval DP to do

However, to minimize the dictionary order

Reverse the LCS, transfer and maintain the F[I][J].S as the minimum optimal solution for the current state dictionary order

The first half of the F[N][N].S must be the first half of the palindrome (think about it)

When the length of S is odd, one more output (because of this length +1, and the dictionary order is guaranteed to be minimal (e.g. Axyzb Bzyxa, is AXB))

////main.cpp//uva11404////Created by Candy on 03/11/2016.//copyright©2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn=1e3+5;CharA[n],b[n];intN;//void dp () {//N=strlen (a+1);//ans=0;//    //for (int i=1;i<=n;i++) f[i][i]=1;//for (int i=n;i>=1;i--) {//f[i][i]=1;//for (int j=i+1;j<=n;j++) {//F[i][j]=max (f[i+1][j],f[i][j-1]);//if (A[i]==a[j]) F[i][j]=max (f[i][j],f[i+1][j-1]+1);//        }//    }//}structdata{intv; strings; Data () {v=0; s="";}} F[n][n];voiddp () { for(intI=1; i<=n;i++) b[i]=a[n-i+1];  for(intI=1; i<=n;i++)         for(intj=1; j<=n;j++){            if(a[i]==B[j]) {F[I][J].V=f[i-1][j-1].v+1; F[i][j].s=f[i-1][j-1].s+A[i]; }Else{                if(f[i][j-1].v>f[i-1][j].v) {f[i][j].v=f[i][j-1].v; F[i][j].s=f[i][j-1].s; }Else if(f[i-1][j].v>f[i][j-1].v) {f[i][j].v=f[i-1][j].v; F[i][j].s=f[i-1][j].s; }Else{f[i][j].v=f[i][j-1].v; F[i][j].s=min (f[i][j-1].s,f[i-1][j].s); }            }        }}intMainintargcConst Char*argv[]) {     while(SCANF ("%s", A +1)!=EOF) {N=strlen (A +1);        DP (); intlen=f[n][n].v; strings=F[n][n].s; if(len&1) {len/=2;  for(intI=0; i<len;i++) Putchar (S[i]);  for(inti=len;i>=0; i--) Putchar (S[i]);//!}Else{len/=2;  for(intI=0; i<len;i++) Putchar (S[i]);  for(inti=len-1; i>=0; i--) Putchar (S[i]); } Putchar ('\ n'); }        return 0;}

UVA 11404 palindromic subsequence[dp LCS Print]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.