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 << ;
const double eps=1e-;
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[][];
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=;i<n;i++){
for(int j=;j<n;j++){
if(s[i]==k[j])dp[i+][j+]=dp[i][j]+;
else dp[i+][j+]=max(dp[i][j+],dp[i+][j]);
}
}
cout<<dp[n][n]<<endl;
}
return ;
}

Uva 11151 - Longest Palindrome的更多相关文章

  1. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. LeetCode 409 Longest Palindrome

    Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...

  4. LeetCode Longest Palindrome

    原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...

  5. 每天一道LeetCode--409 .Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  6. 24. leetcode 409. Longest Palindrome

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  7. [Swift]LeetCode409. 最长回文串 | Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  9. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

随机推荐

  1. Jenkins系列之Jenkins的安装(一)

    自动化测试的时候通常我们都会进行持续集成,下面是持续集成工具Jenkins的安装 Jenkins优点: 开源免费 跨平台,支持所有的平台 web形式的可视化的管理页面 安装配置超级简单 tips及时快 ...

  2. svn更新报错Please execute the 'Cleanup' command.

    更新svn报错 要Clearnup一下就可以再更新了 点击svn中 clear up ok之后恢复正常

  3. crontab定时清理日志

    1.创建shell脚本 vi test_cron.sh #!/bin/bash#echo "====`date`====" >> /game/webapp/test_c ...

  4. spark学习(3)---集合

    一.list https://blog.csdn.net/xianpanjia4616/article/details/84930779 华为文档:https://support.huawei.com ...

  5. @Inherited注解

    允许子类继承父类的注解 https://blog.csdn.net/renli2549/article/details/78432272

  6. linux shell脚本学习笔记一

    一.文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tm ...

  7. Ztree加载完成后显示勾选节点

    ①前言:这个在度娘上没有搜到解决的办法,于是自己查看了ztree的API,发现其实还是比较简单的.做个笔记以备不时之需. ②需求: 像下图一样,在加载完成之后就显示需要勾选的项. ③解决方案: 首先页 ...

  8. 【ssm】spring功能讲解

    概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...

  9. apache 添加虚拟机

    <VirtualHost *:80> DocumentRoot "E:/UPUPW_AP7.0/htdocs/xd.local/public" ServerName a ...

  10. Keil uVision “已停止工作”

    之前一直受这个问题的困扰,但是因为只是下载程序,下载镜像文件完事就算了.随便keil挂掉. 今天要调试程序,发现开启调试keil就挂掉了,烦. 解决办法参见: http://218.244.144.1 ...