UVA 11404 五 Palindromic Subsequence

Palindromic Subsequence

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 11404

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <string>
 5 #include <iostream>
 6 using namespace std;
 7
 8 struct Node
 9 {
10     int len;
11     string str;
12 }dp[1005][1005];
13
14 int main()
15 {
16     int l;
17     char str1[1005],str2[1005];
18     int i,j,k;
19     while(scanf("%s",str1+1)!=EOF)
20     {
21         l=strlen(str1+1);
22         for(i=1;i<=l;i++)
23             str2[i]=str1[l-i+1];
24
25         for(i=0;i<=l;i++)
26             dp[0][i].len=0,dp[i][0].len=0,dp[0][i].str="",dp[i][0].str="";
27
28         for(i=1;i<=l;i++)
29         {
30             for(j=1;j<=l;j++)
31             {
32                 if(str1[i]==str2[j])
33                 {
34                     dp[i][j].len=dp[i-1][j-1].len+1;
35                     dp[i][j].str=dp[i-1][j-1].str+str1[i];
36                 }
37                 else
38                 {
39                     if(dp[i][j-1].len>dp[i-1][j].len)
40                     {
41                         dp[i][j].len=dp[i][j-1].len;
42                         dp[i][j].str=dp[i][j-1].str;
43                     }
44                     else if(dp[i-1][j].len>dp[i][j-1].len)
45                     {
46                         dp[i][j].len=dp[i-1][j].len;
47                         dp[i][j].str=dp[i-1][j].str;
48                     }
49                     else
50                     {
51                         dp[i][j].len=dp[i-1][j].len;
52                         dp[i][j].str=min(dp[i-1][j].str,dp[i][j-1].str);
53                     }
54                 }
55             }
56         }
57
58         int n=dp[l][l].len;
59         string ans=dp[l][l].str;
60
61         if(n%2==0)
62         {
63             for(i=0;i<n/2;i++)
64             {
65                 printf("%c",ans[i]);
66             }
67             for(i=n/2-1;i>=0;i--)
68             {
69                 printf("%c",ans[i]);
70             }
71         }
72         else
73         {
74             for(i=0;i<n/2;i++)
75             {
76                 printf("%c",ans[i]);
77             }
78             for(i=n/2;i>=0;i--)
79             {
80                 printf("%c",ans[i]);
81             }
82         }
83         printf("\n");
84     }
85     return 0;
86 }

时间: 2024-10-13 01:17:19

UVA 11404 五 Palindromic Subsequence的相关文章

UVA 11404 Palindromic Subsequence[DP LCS 打印]

UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求LCS,转移同时维护f[i][j].s为当前状态字典序最小最优解 f[n][n].s的前半部分一定是回文串的前半部分(想想就行了) 当s的长度为奇时要多输出一个(因为这样长度+1,并且字典序保证最小(如axyzb  bzyxa,就是axb)) // // main.cpp // uva11404 //

LPS UVA 11404 Palindromic Subsequence

题目传送门 1 /* 2 LPS(Longest Palidromic Subsequence):最长回文子序列,和回文串不同,子序列是可以不连续的. 3 转化为LCS问题,将字符串逆序,然后和本串求LCS就是LPS的长度(为啥不就是LPS?),但是前一半是LPS的一半,可以补全 4 推荐->还有一种写法是用了LCS的思想,dp[i][j]表示i到j的最长回文串长度,状态转移方程: 5 1. dp[j][j+i-1] = dp[j+1][j+i-2] + 2; (str[j] == str[j+

uva 11404 dp

UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串,他可能是[l+1, r] 和[l, r-1]两个区间的结果.或者当s[l] == s[r]时,区间[l+1, r-1]的结果再加上以s[l], s[r]为首尾的子序列. dp[l][r] = ans(dp[l][r-1], dp[l+1][r], s[l] + dp[l+1][r-1] + s[r]) dp存的是子序列,即一个s

LeetCode 516. Longest Palindromic Subsequence

516. Longest Palindromic Subsequence Add to List Description Submission Solutions Total Accepted: 2159 Total Submissions: 5216 Difficulty: Medium Contributors: Stomach_ache Given a string s, find the longest palindromic subsequence's length in s. You

*Maximum Length Palindromic Sub-Sequence of an Array.

/* Write a function to compute the maximum length palindromic sub-sequence of an array. A palindrome is a sequence which is equal to its reverse. A sub-sequence of an array is a sequence which can be constructed by removing elements of the array. Ex:

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

Longest Palindromic Substring &amp; Longest Palindromic Subsequence

5. Longest Palindromic Substring 题目链接:https://leetcode.com/problems/longest-palindromic-substring/#/description 题目大意:给定一个字符串s,返回该字符串的最长回文子串.s的最大长度不超过1000. 思路:对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙)往两边同时进行扫描,直到不是回文串为止.假设字符串的长度为n

UVA 10405 Longest Common Subsequence

最长公共子系列,简单的dp,不过注意有空格,所以,在读字符串的时候,尽量用gets读,这样基本没问题 #include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int dp[1001][1001]; int MAX(int x,int y) { if (x>y) return x; else return y; } int ma

516. Longest Palindromic Subsequence

本周课堂上学习的是动态规划,因此在LeetCode上找到相应的题进行练习. 题目: Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000. (给定一个字符串S,找到它的最长回文子序列长度) 示例: Example 1:Input  :"bbbab" Output:4 (One possibl