Uva 11151 - Longest Palindrome
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的更多相关文章
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode Longest Palindrome
原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...
- 每天一道LeetCode--409 .Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- [LeetCode&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- Stack frame
http://en.citizendium.org/wiki/Stack_frame In computer science, a stack frame is a memory management ...
- 事件的节流(throttle)与防抖(debounce)
事件的节流(throttle)与防抖(debounce) 有些浏览器事件可以在短时间内快速触发多次,比如调整窗口大小或向下滚动页面.例如,监听页面窗口滚动事件,并且用户持续快速地向下滚动页面,那么滚动 ...
- jekyll本地环境搭建(Windows)
序:最近一直在搞Github建站,所以一直没机会写文章,那边的环境虽然搞好了,但是网站的界面却是个问题,不想用别人的,总想自己设计个,却感觉没经验吧,就一直耽搁了.所以也就没心情在那边写文章,很久没写 ...
- 【2018百度之星资格赛】F 三原色图 - 最小生成树
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6349 Knowledge Point: 最小生成树算法Prim&Kruskal Summari ...
- 洛谷——P1475 控制公司 Controlling Companies
P1475 控制公司 Controlling Companies 题目描述 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.(此处略去一句废话)据说,如果至少满足了以下三个条 ...
- LinuxMint19.1安装搜狗拼音输入法
Installation 1.到搜狗拼音输入法官网下载Linux版. 2.使用dpkg命令安装deb软件包 $ sudo dpkg -i sogoupinyin_2.2.0.0108_amd.deb ...
- Vim常用快捷键--正常的学习曲线
vim可能对于初学者不太友好,学习曲线有点陡,特此整理了较为平滑的学习曲线的学习快捷键的方式,包含最常用的快捷键,让初学者领悟vim的优点,想要进阶学习请查找其它更好的教程 正常模式:可以使用快捷键命 ...
- 00 大王警语--be_a_new_gentleman
大王博客:https://www.cnblogs.com/alex3714/ # 表面层次# 1,着装特体(服饰的牌子中高端)# 2,每天洗澡# 3,适当用香水# 4,女士优先# 5,不随地吐痰.不乱 ...
- HYSBZ - 1050(旅行comf 并查集Java实现)
HYSBZ - 1050(旅行comf Java实现) 原题地址 解法:枚举每一条边,对于这条边,我们需要找到集合中和其值相差最小的最大边,这个集合是指与包括i边在内的ST联通集.对于这一要求,我们只 ...
- Codeforces 989C - A Mist of Florescence
传送门:http://codeforces.com/contest/989/problem/C 这是一个构造问题. 构造一张网格,网格中的字符为’A’.’B’.’C’.’D’,并且其连通块的个数分别为 ...