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. Extensions can add new functionality to a type, but they cannot override existing functionality.

    Extensions can add new functionality to a type, but they cannot override existing functionality.

  2. Application received signal SIGSEGV

    Application received signal SIGSEGV (null) (( 0 CoreFoundation 0x0000000181037d50 <redacted> + ...

  3. CSS 命名规范 BEM 思想

    Part.1 何为 BEM? BEM :Block ( 块 ) 丶Element ( 元素 ) 丶Modifier ( 修饰符 ) 出 处:是由 Yandex 团队提出的一种前端命名方法论 优 点:命 ...

  4. JAVA I/O之文件复制

    有没有大佬告诉我这个不要了的代码插入区(就现在这句话的区域)怎么删掉....... //一个字节一个字节的复制 public static void fun() throws IOException ...

  5. js 的静态获取和动态获取

    静态获取方法 document.getElementById obj.querySelector obj.querySelectorAll 动态获取方法(每次使用时候会回去重新获取一次) obj.ge ...

  6. centOS目录结构介绍

    Linux / CentOS目录结构 /: 根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin: ...

  7. 05 Python运算符

    Python运算符: 此图来源于菜鸟教程,更详细参考 http://www.runoob.com/python3/python3-basic-operators.html 说明: 同一优先级的通常从左 ...

  8. 实验1“C语言开发环境使用和数据类型、运算符、表达式”总结与体会

    一.实验结论 1.判断奇偶 // 程序功能: // 要求用户从键盘输入一个整数,判断其是奇数还是偶数 #include <stdio.h> int main() { int x; prin ...

  9. chrome浏览器中解决embed标签 loop="true" 背景音乐无法循环的问题。

    今天试了下在html网页中加入背景音乐并设置为循环播放.一开始用<embed>标签,设置loop="true", 但是结果发现在IE浏览器可以,但是在chrome浏览器 ...

  10. DESEncrypt对称加密解密

    分享一个很好用的DESEncrypt对称加密解密的类 using System; using System.Security.Cryptography; using System.Text; usin ...