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


相关推荐

  • strm().filter().collect()和stream().map().collect()的作用

    strm().filter().collect()和stream().map().collect()的作用在看代码的时候看到了一下

    2025年6月19日
    3
  • 数据预处理的一些知识「建议收藏」

    数据预处理的一些知识「建议收藏」数据预处理的一些知识做研究时只要与数据分析相关就避免不了数据预处理。我们常见的预处理包括:标准化(规范化),归一化,零均值(化),白化,正则化……这些预处理的目的是什么呢?网上查的总是零零散散,很难搞清楚。因此我用此片博客来总结下。借鉴其他博客的内容,可能未一一注明还请谅解。一,数据标准化目的:为了消除量纲影响和变量自身数值大小的影响,方便统计处理(尤其是加权),故将数据标准化。例如:我们对

    2025年6月1日
    3
  • 关于SetCapture() 和 ReleaseCapture()的用法的个人理解[通俗易懂]

    关于SetCapture() 和 ReleaseCapture()的用法的个人理解[通俗易懂]1.函数功能:在当前线程的指定窗口里设置鼠标捕获。一旦窗口捕获了鼠标,所有鼠标输入都针对该窗口,无论光标是否在窗口的边界内还是边界外。同一时刻只能有一个窗口捕获鼠标。2.失效条件: A.当鼠标在其他窗口按下;B.调用ReleaseCapture释放。3. SetCapture和ReleaseCapture必须成对出现通俗来讲,例如:一只羊被一根弹性的

    2022年5月3日
    75
  • JS数组合并(5种)[通俗易懂]

    JS数组合并(5种)[通俗易懂]前言项目过程中,经常会遇到JS数组合并的情况,时常为这个纠结。这里整理一下。简单而实用的for最容易想到的莫过于for了。会变更原数组,当然也可以写成生成新数组的形式。letarr=[1,2]letarr2=[3,4]for(letiinarr2){arr.push(arr2[i])}console.log(arr)//[1,2,3,4]arr.concat(arr2)会生成新的数组。letarr=[1,2]let

    2022年6月30日
    41
  • 在centos服务器安装MySQL数据库详细步骤

    在centos服务器安装MySQL数据库详细步骤

    2021年9月26日
    65
  • java最长递增子序列_求数组中最长递增子序列「建议收藏」

    java最长递增子序列_求数组中最长递增子序列「建议收藏」什么是最长递增子序列呢?问题描述如下:设L=是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=,其中k1对于这个问题有以下几种解决思路:1、把a1,a2,…,an排序,假设得到a’1,a’2,…,a’n,然后求a的a’的最长公共子串,这样总的时间复杂度为o(nlg(n))+o(n^2)=o(n^2);2、动态规划的思路:另设一辅助数组b,定义b[n]表示以a[n]结尾的最长递增…

    2022年4月30日
    55

发表回复

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

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