uva 11151 Longest Palindrome (最长公共子序列)

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 and Output

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.

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

题目大意:求给出的字符串中最长的回文串。

解题思路:字符串的正序和逆序求最长公共子序列。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
char s[1005], s2[1005];
int dp[1005][1005], len;
void DP() {
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= len; i++) {
        for (int j = 1; j <= len; j++) {
            if (s[i - 1] == s2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
}
int main() {
    int T;
    scanf("%d%*c", &T);
    while (T--) {
        gets(s);
        len = strlen(s);
        for (int i = len - 1; i >= 0; i--) {
            s2[i] = s[len - i - 1];
        }
        DP();
        printf("%d\n", dp[len][len]);
    }
    return 0;
}
时间: 2024-08-10 02:10:18

uva 11151 Longest Palindrome (最长公共子序列)的相关文章

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 al

poj1159 Palindrome(最长公共子序列)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52966   Accepted: 18271 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a

POJ1159——Palindrome(最长公共子序列+滚动数组)

Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inser

uva 10192 Vacation(最长公共子序列)

uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you really don't know which cities you should visit. So, you ask your parents for help. Your mother says "My son, you MUST visit Paris, Madrid, Lisboa an

UVa 10192 Vacation (最长公共子序列)

Vacation Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   The Problem You are planning to take some rest and to go out on vacation, but you really don't know which cities you should visit. So, you ask your paren

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

uva 10405 Longest Common Subsequence(最长公共子序列)

经典的最长公共子序列问题,我刚开始用string敲的,就是为了练练手,没想到竟然wa了,还以为我用错了呢...换了字符数还是wa...真无语,这么简单的题快把我弄糊涂了,后来听人说是输入可能有空格...这是巨坑啊,题上都没说清楚,白白wa了几发...就是设一个数组d[i][j]遍历两个字符数组当a[i]==b[j]的时候d[i][j]=d[i-1][j-1]+1.不相等的时候就是d[i][j]=max(d[i-1][j],d[i][j-1]).别忘了初始化.真坑 代码: #include<ios

UVA 10100- Longest Match(dp之最长公共子序列)

题目地址:UVA 10100 题意:求两组字符串中最大的按顺序出现的相同单词数目. 思路:将字串中的连续的字母认作一个单词,依次计算出两个字符串中的单词,其中第1个字符串的单词序列为t1.word[1]-..t1.word[n],第2个字符串的单词序列为t2.word[1]-..t2.word[m].然后将每个单词当成一个字符,使用LCS算法计算出两个字符串的最长公共子序列,该序列的长度就是最长匹配. #include <stdio.h> #include <math.h> #in

Longest Common Substring(最长公共子序列)

Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37 Accepted Submission(s): 28   Problem Description Given two strings, you have to tell the length of the Longest Common Su