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;
    }
  • 相关阅读:
    USACO--2.1The Castle
    浅谈python字符串存储形式
    面向对象——一起来复习托付与事件!
    数据结构——算法之(032)(求两个串中的第一个最长子串)
    读《浪潮之巅》有感
    关于android 怎样安装 assets文件下的apk
    每日一小练——求质数
    怎样使破解网页的禁止复制黏贴
    Angularjs Nodejs Grunt 一个样例
    《TCP/IP具体解释卷2:实现》笔记--域和协议
  • 原文地址:https://www.cnblogs.com/pk28/p/5936538.html
Copyright © 2011-2022 走看看