zoukankan      html  css  js  c++  java
  • 回文子序列 (Palindromic Subsequence,UVa 11404)

     1 #include <iostream>
     2 #include <string.h>
     3 #include <string>
     4 #include <fstream>
     5 #include <algorithm>
     6 #include <stdio.h>
     7 #include <vector>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 using namespace std;
    12 const double eps = 1e-8;
    13 const int INF=0x7fffffff;
    14 #define MAXN 1002
    15 string s1,s2;
    16 string Reverse(string str1)
    17 {
    18     string str2="";
    19     for(int i=str1.length()-1;i>=0;i--)
    20     str2+=str1[i];
    21     return str2;
    22 }
    23 struct node
    24 {
    25     int len;
    26     string s;
    27     friend bool operator < (const node &op1, const node &op2)
    28     {
    29         return op1.len == op2.len? op1.s > op2.s: op1.len < op2.len;
    30     }
    31 }d[MAXN][MAXN];
    32 node ans;
    33 bool check(string x)
    34 {
    35     int i=0,j=0;
    36     while(i<x.length()&&j<x.length())
    37     {
    38         if(x[i]==s1[j]){i++;j++;}
    39         else j++;
    40     }
    41     if(i<x.length())return false;
    42     return true;
    43 }
    44 int main()
    45 {
    46     while(cin>>s1)
    47     {
    48 
    49         s2=Reverse(s1);
    50         int len=s1.length();
    51         s1='*'+s1;
    52         s2='*'+s2;
    53 
    54         for(int i=0;i<=len;i++)
    55         {d[i][0].len=d[0][i].len=0;d[i][0].s=d[0][i].s="";}
    56         for(int i=1;i<=len;i++)
    57         for(int j=1;j<=len;j++)
    58         {
    59         if(s1[i]==s2[j])
    60         {
    61             d[i][j].len=d[i-1][j-1].len+1;
    62             d[i][j].s=d[i-1][j-1].s+s1[i];
    63             //cout<<ans[i-1][j-1].s<<'*';
    64         }
    65         else{
    66             if(d[i][j-1] < d[i-1][j])
    67                 d[i][j] = d[i-1][j];
    68             else
    69                 d[i][j] = d[i][j-1];
    70         }
    71         //cout<<ans[i][j].s<<endl;
    72         }
    73         ans.len = 0;
    74         ans.s.clear();
    75         for(int i = 1; i < len; ++i)
    76             if(ans< d[i][len-i])
    77                 ans = d[i][len-i];
    78         ans.len *= 2;
    79         node temp;
    80         for(int i = 0; i < len; ++i)
    81         {
    82             temp = d[i][len-i-1];
    83             temp.len = temp.len*2+1;
    84             temp.s += s1[i+1];
    85             if(ans < temp)
    86                 ans = temp;
    87         }
    88 
    89         if(d[len][len].len%2)ans.s+=Reverse(ans.s).substr(1,d[len][len].len/2);
    90         else ans.s+=Reverse(ans.s);
    91         cout<<ans.s<<endl;
    92     }
    93     return 0;
    94 }

    反序LCS

  • 相关阅读:
    解读dbcp自动重连那些事
    dbcp重连问题排查
    Redis 历史版本下载URL
    jackson 完整Jar包
    Linux缺少动态连接库.so--cannot open shared object file: No such file or directory
    多模匹配--历程
    利用poi操作word文档
    Java word 内容读取
    光纤存储和服务器架构总结
    FastDFS和集中存储方式对比
  • 原文地址:https://www.cnblogs.com/TO-Asia/p/3201460.html
Copyright © 2011-2022 走看看