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;
    }
  • 相关阅读:
    3-剑指Offer: 连续子数组的最大和
    2-剑指offer: 最小的K个数
    1-剑指offer: 数组中出现次数超过一半的数字
    django中运行定时任务脚本
    django+sqlite进行web开发(二)
    django+sqlite3进行web开发(一)
    TL-WDN5200H无线usb网卡在Linux上的使用
    比较好用的C++11在线编译器
    MarkDown中如何加入上标和下标
    3. 卷积神经网络(CNN)
  • 原文地址:https://www.cnblogs.com/pk28/p/5936538.html
Copyright © 2011-2022 走看看