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


相关推荐

  • Python Flow control[通俗易懂]

    Python Flow control[通俗易懂]HowtotellPythontomakeintelligentdecisionsaboutwhatcodetorun,whatcodetoskip,andwhatcodetorepeatbasedonthevaluesithas.WithFlowcharts!Flowcontrolwillneedyesornooptionstomakedecisions.InPythoncode,yesandnoisshownas

    2022年5月29日
    36
  • 写辅助脚本违法吗_网络游戏里的成功几率

    写辅助脚本违法吗_网络游戏里的成功几率转至http://www.cppblog.com/elva/archive/2008/02/19/42924.html一、前言  所谓游戏外挂,其实是一种游戏外辅程序,它可以协助玩家自动产生游戏动作、修改游戏网络数据包以及修改游戏内存数据等,以实现玩家用最少的时间和金钱去完成功力升级和过关斩将。虽然,现在对游戏外挂程序的“合法”身份众说纷纭,在这里我不想对此发表任何个人意见,让时间…

    2022年10月8日
    3
  • Spring中ApplicationContext对Beanfactory扩展[通俗易懂]

    Spring中ApplicationContext对Beanfactory扩展[通俗易懂]ApplicationContext比BeanFactory扩展了高级特性,除了集成了ListableBeanFactory和HierarchicalBeanFactory以外,实现了如下附加功能:

    2022年6月24日
    31
  • 最新QT下载和安装 指南教程「建议收藏」

    最新QT下载和安装 指南教程「建议收藏」原文地址:http://c.biancheng.net/view/3851.htmlQt体积很大,有1GB~3GB,官方下载通道非常慢,相信很多读者会崩溃,所以建议大家使用国内的镜像网站(较快),或者使用迅雷下载(很快)。作为Qt下载教程,本文会同时讲解以上三种下载方式。Qt官方下载(非常慢)Qt官网有一个专门的资源下载网站,所有的开发环境和相关工具都可以从这里下载,具体地址是:http://download.qt.io/图1:Qt官方下载网站截图对目录结构的…

    2022年5月17日
    39
  • postman升级后,collection集合中的接口找不到了

    postman升级后,collection集合中的接口找不到了

    2022年2月15日
    57
  • linux系统查看版本命令,Linux系统查看系统版本命令[通俗易懂]

    linux系统查看版本命令,Linux系统查看系统版本命令[通俗易懂]以下操作在centos系统上实现,有些方式可能只适用centos/redhat版本系统uname-a|uname-r查看内核版本信息[root@node1~]#uname-aLinuxnode12.6.32-573.el6.x86_64#1SMPThuJul2315:44:03UTC2015x86_64x86_64x86_64GNU/Linux[root@n…

    2022年8月21日
    15

发表回复

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

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