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. swift extension 的最终目的是结合

    与数据结合:对数据进行操作: 与行为结合:使用原有行为生成更便捷的行为: 与协议结合:实现协议: 与类型结合:对类型数据进行操作,添加新的行为: 与关联类型.泛型结合:对类型做出限定.

  2. MFC窗体大小变化

    对话框的大小变化后,假若对话框上的控件大小不变化,看起来会比较难看.下面就介绍怎么让对话框上的控件随着对话框的大小的变化自动调整. 首先明确的是Windows有一个WM_SIZE消息响应函数,这个函数 ...

  3. Eigen库笔记整理(二)

    Eigen/Geometry 模块提供了各种旋转和平移的表示 旋转矩阵直接使用 Matrix3d 或 Matrix3f Eigen::Matrix3d rotation_matrix = Eigen: ...

  4. 3D NAND闪存是个啥?让国内如此疯狂

    Repost: https://news.mydrivers.com/1/477/477251.htm 上个月底武汉新芯科技主导的国家级存储器产业基地正式动工,在大基金的支持下该项目将投资240亿美元 ...

  5. document.write() 和 document.writeln区别

    document.write() 和 document.writeln 都是JavaScript向客户端写入的方法,writeln是以行方式输出的,但并不是指页面实际效果中的换行,两种方法在查看源代码 ...

  6. Python学习——filter&map

    filter&map 1.filter函数 filter()函数用于过滤序列,过滤掉不符合条件的元素,Python3以后返回一个迭代器对象(可以用list()转化为列表查看). filter( ...

  7. LES on Wind turbine

  8. BZOJ 5106 [CodePlus2017]汀博尔

    [题解] 二分答案.r要设好,不能随便设置为max(s,len),不然check的时候会爆long long #include<cstdio> #include<algorithm& ...

  9. 消息传递(cogs 1001)

    问题描述WZland开办了一个俱乐部(这里面可以干任何的事情),这引来了许多的人来加入.俱乐部的人数越来越多,关系也越来越复杂……俱乐部的人来自各个地方,为了增加友谊,俱乐部举行了一次晚会.晚会上又进 ...

  10. SSH三种框架及表示层、业务层和持久层的理解(转)

    Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...