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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux3.10.0块IO子系统流程(7)– 请求处理完成

    Linux3.10.0块IO子系统流程(7)– 请求处理完成

    2022年4月3日
    40
  • java的反射机制带来的好处_线程安全与线程不安全

    java的反射机制带来的好处_线程安全与线程不安全什么是反射Java的反射(reflection)机制是指在程序的运行状态中,可以构造任意一个类的对象,可以了解任意一个对象所属的类,可以了解任意一个类的成员变量和方法,可以调用任意一个对象的属性和方法jdbc(数据库连接技术)在加载驱动时运用到了反射技术例如:实例化对象第一种:Personp=newPerson()虚拟机在执行的时候已经确切知道要实例化哪个类的对象第二种:反射:虚拟机在实例化对象的时候,可以事先不知道要实例化哪个类的对象,传参的时候虚拟机根据参数确定要实例化哪个类的

    2022年8月24日
    6
  • 磁力链接文件服务器,什么是磁力链接(BT、磁力链这些词语是什么意思?)

    磁力链接文件服务器,什么是磁力链接(BT、磁力链这些词语是什么意思?)“知其然知其所以然”。我们经常在下载资料的时候能看到BT、磁力链等词语,百思特网这些词语到底是什么意思呢?下载都会用,但是你了解吗?BT下载传统的下载模式是每个客户端从服务器拷贝文件,跟校园内常用的FTP一样。因为服务器宽带是一定的,所以下载的人越多下载速度会越慢。而现在使用的下载器情况正好相反,使用的人越多文件下载速度越快。这是因为现在的下载器普遍采用类似BT的下载方式。布拉姆科恩发明了BT协议…

    2022年8月10日
    13
  • 堆排序实现及应用

    堆排序实现及应用

    2022年2月6日
    46
  • pythonconsole使用_pycharm add new configuration

    pythonconsole使用_pycharm add new configurationPycharm的下方工具栏中有两个窗口:PythonConsole和Terminal(如下图)Terminal叫做终端,即命令行模式(命令行模式与系统的CMD(命令提示符)一样,可以运行各种系统命令);PythonConsole叫做Python控制台,即Python交互模式(Python交互模式主要有两种:CPython用>>>作为提示符,而IPython用In[序号]:作为提示符)。Python交互式模式可以直接输入代码,然后执行,并立刻得到结果,因此Pytho

    2022年8月26日
    7
  • 闭包面试回答_ajax面试题

    闭包面试回答_ajax面试题写在前面:在学习了闭包之后,试着做做这些题。其实是一种很棒地检验自己学习成果的手段。我当时反反复复,学了但好像又没完全学,遇到题还是一头雾水,到现在可以捋得很清楚也经历了蛮久的。而且从this,执行上下文,作用域一直走过来,这些题目涉及的内容也相对全面,加油喽。

    2022年8月30日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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