zoukankan      html  css  js  c++  java
  • Uva 11151

    A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-palindromic string, you can always take away some letters, and get a palindromic subsequence. For example, given the string ADAM, you remove the letter M and get a palindrome ADA. Write a program to determine the length of the longest palindrome you can get from a string.

    Input

    The first line of input contains an integer T (≤ 60). Each of the next T lines is a string, whose length is always less than 1000. For ≥ 90% of the test cases, string length ≤ 255.

    Output

    For each input string, your program should print the length of the longest palindrome you can get by removing zero or more characters from it.

    Sample Input

    2

    ADAM

    MADAM

    Sample Output

    3

    5

    注意有空格。

    LCS 最长公共子序列

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/10/7 19:14:31
    File Name     :uva11151.cpp
    ************************************************ */
    #include <bits/stdc++.h>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int dp[1100][1100];
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int t,n;
        cin>>t;
        string s,k;
        getchar();
        while(t--){
            getline(cin,s);
            k=s;
            reverse(s.begin(),s.end());
            int n=s.size();
            cle(dp);
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(s[i]==k[j])dp[i+1][j+1]=dp[i][j]+1;
                    else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
                }
            }
            cout<<dp[n][n]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    L2R 三:常用工具包介绍之 XGBoost与LightGBM
    连续特征自动离散化
    优化算法
    L2R 一:基础知识介绍
    深度学习--pytorch安装
    vim实用操作指南
    小贴士--Python
    setInterval和clearInterval应用小实例
    表单应用举例
    容器
  • 原文地址:https://www.cnblogs.com/pk28/p/5936538.html
Copyright © 2011-2022 走看看