uva 11151 Longest Palindrome (最长公共子序列)[通俗易懂]

uva 11151 Longest Palindrome (最长公共子序列)[通俗易懂]uva11151LongestPalindromeApalindromeisastringthatreadsthesamefromtheleftasitdoesfromtheright.Forexample,I,GAGandMADAMarepalindromes,butADAMisnot.Here,weconsideralso

大家好,又见面了,我是你们的朋友全栈君。

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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157216.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • win+r常用指令怎么打开_R语言指令

    win+r常用指令怎么打开_R语言指令最近在学习Linux,被命令行深深吸引了,陷入其中不能自拔,考虑到Windows上也有cmd命令行,但对新人来说不是很友好。这次我们就先讲一下Win+R运行框里的快捷键,绝对能提高不少效率!什么是Win+R防止有些小白看不懂,所以说明一些,使用Windows+R快捷键就可以打开如下图的运行窗口,在里面输入命令可以方便快捷地打开很多东西,而且本文的所有操作都是在这个运行框里输入的,不要与cm…

    2022年10月12日
  • Springboot项目搭建(前端到数据库,超详细)

    Springboot项目搭建(前端到数据库,超详细)下面详细谈谈我的第一个springboot项目搭建,希望会给还在摸索的同学一点帮助。项目说明:开发环境:Eclipse4.42框架:Springboot工具:Maven前端:Html、Thymeleaf后台:Hibernate数据库:Mysql为什么要搭建Springboot项目?教科书式的阐述这里就不说了,我就总结为两个词语“简单、方便”。为了更…

  • 哥尼斯堡七桥问题解法_酒分之一实验室

    哥尼斯堡七桥问题解法_酒分之一实验室 JOJ1200Jugs题目链接:http://acm.jlu.edu.cn/joj/showproblem.php?pid=1200题目的意思是,有两个容器,容量分别为ca和cb,cacb,初始时两个容器都是空的,水无限量供应,问如何用这两个容器量出n单位的水放在容量为cb的那个容器中?这个题目给出的数

  • 何时使用数据库存储过程

    何时使用数据库存储过程

  • Dirsearch_torrentsearch下载

    Dirsearch_torrentsearch下载dirsearch下载下载网址:https://github.com/maurosoria/dirsearch下图是下载好的文件夹这样就下载好了我在使用的时候出现了下面的这个问题百度了很久也没有找到,kali也不太会用,就继续找继续找,终于????,解决办法找到了!!!是用户权限的问题!依然对dirsearch修改用户权限还是在属性->安全里面选择想要添加的用户,并允许该用户完全控制如下图…

  • 鼠标滑过显示图片大图效果

    鼠标滑过显示图片大图效果

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号