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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • O2O:互联网的另一种变异形式

    O2O:互联网的另一种变异形式

    2021年8月27日
  • 图片连续循环滚动代码(向上、下、左、右)

    图片连续循环滚动代码(向上、下、左、右)向上:向下:向左:向右:

    2022年7月1日
  • MicroBlaze使用_char* malloc

    MicroBlaze使用_char* malloc转自http://blog.163.com/gcs_gcs/blog/static/17448606620121193113914/在最近的工程中,需要用到PS/2键盘和鼠标作为控制输入,所以在网上找了一些相关的资料,内容很丰富,看来已经有很多人做过了这方面的编程。本篇Blog算是实践总结,为以后的开发积累一些基础知识。MicroBlaze支持重启(reset),中断(interrupt),暂…

    2022年10月22日
  • C语言getchar的用法_getchar的用法

    C语言getchar的用法_getchar的用法getchargetchar的定义MSDN中的解释是从流中读取一个字符intch=getchar()//通过getchar获取字符的ASCII码值传到ch里面存储这里注意:getchar获取字符以后通过ASCII码值的形式存储在ch里为什么不用charch?getchar读到的是一个int类型的数据好处是返回值的时候非常方便接下来我们再看一个示例:intmain(){intch=0;while((ch=getch.

    2022年10月19日
  • 贵金属黄金投资基础知识及技术交易篇「建议收藏」

    贵金属黄金投资基础知识及技术交易篇「建议收藏」贵金属黄金投资基础知识及技术交易篇  一、影响贵金属价格波动的因素  我们通常所说的贵金属主要指黄金和白银,由于它们的属性有很多共同点,所以价格走势基本相同,影响价格的因素也大同小异,皇玛hmcfds贵金属交易平台小编讲讲贵金属交易的基础知识:  1、供需关系:任何商品价格的波动本质因素都是供需关系的变化,贵金属也不例外。如果产量大幅增加就可能导致贵金属价格的下跌,如果投资交易、实物首…

    2022年5月8日
  • StackOverflow 提问艺术[通俗易懂]

    StackOverflow 提问艺术[通俗易懂]在黑客的世界里,当你拋出一个技术问题时,最终是否能得到有用的回答,往往取决于你所提问和追问的方式。本指南将教你如何正确的提问以获得你满意的答案。https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/master/README-zh_CN.md转载于:https://www.cnblogs.com/s…

    2022年6月23日

发表回复

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

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