UVA 11404 plalidromic subsquence

Source: Internet
Author: User
Tags define local

The longest palindrome sequence can be obtained by solving the original string s and the LCS of the reverse serial RV, because the palindrome string is required to be dp[i][j, and the length is preserved.

Requires a minimum dictionary order, Dp[i][j] should represent the end of a palindrome string, so the boundary is a single character, that is, i+j=len+1.

The most troublesome part of the problem lies in the dictionary order, I wrote a comparison function, a bit violent (constant large).

It is also possible to reverse the definition, when the node is going to hold the string of that state (this defines a small constant when comparing the dictionary order)

#include <bits/stdc++.h>using namespacestd;#defineMP Make_pair#defineFi first#defineSe SecondConst intLEN = 1e3+5;CharS[len],rv[len];intDp[len][len];p air<int,int>Pre[len][len];CharVal[len][len];inlinevoidUpdata (intIintJintVCharCConstpair<int,int> &PRV) {Dp[i][j]=v; PRE[I][J]=PRV; VAL[I][J]=C;}ConstAuto Nil = MP (0,0);#defineDim (x) [x.fi][x.se]BOOLCmplex (pair<int,int> a,pair<int,int>b) {    //     while(A! = Nil && val[a.fi][a.se] = =val[b.fi][b.se]) {a= Pre Dim (a); b =Pre Dim (b); }    returnVal Dim (A) <Val Dim (b);}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif     while(gets (s)) {intLen =strlen (s);  for(inti =0; i < Len; i++) {Rv[len-1-I.] =S[i]; }        intHD1,HD2,VL =0;  for(inti =1; I <= Len; i++){            intj = len+1-i; DP[I][J]=1; VAL[I][J] = s[i-1]; PRE[I][J] =Nil; if(Dp[i][j] > VL | | (Dp[i][j] = = VL && Cmplex (MP (I,J), MP (HD1,HD2)))) {//VL =Dp[i][j]; HD1= i; HD2 =J; }             for(intK =1; K < J; k++) Dp[i][k] =0;  forJ <= Len; j + +){                if(s[i-1] = = rv[j-1]) {updata (i,j,dp[i-1][j-1]+2, s[i-1],make_pair (I-1, J-1)); if(Dp[i][j] > VL | | (Dp[i][j] = = VL && Cmplex (MP (I,J), MP (HD1,HD2)))) {//VL =Dp[i][j]; HD1= i; HD2 =J; }                }Else {                    if(dp[i-1][J] > dp[i][j-1] || (dp[i-1][J] = = dp[i][j-1] && Cmplex (MP (i-1, j), MP (i,j-1)) ) ){//Updata (i,j,dp[i-1][j],val[i-1][j],pre[i-1][j]); }Else{updata (i,j,dp[i][j-1],val[i][j-1],pre[i][j-1]); }                }            }        }        intPV = (vl+1) >>1, LN =VL; Auto U=MP (HD1,HD2);  for(inti =0; I < PV; i++) {S[i]=val[u.fi][u.se]; U=pre[u.fi][u.se]; } S[ln]=' /';  for(inti = PV; i < ln; i++) {S[i]= s[ln-1-i];    } puts (s); }    return 0;}

UVA 11404 plalidromic subsquence

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.