1092 回文字符串(LCSL_DP)

1092 回文字符串

基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题

收藏

关注

回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。每个字符串都可以通过向中间添加一些字符,使之变为回文字符串。

例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba。方案1只需要添加2个字符,是所有方案中添加字符数量最少的。

Input

输入一个字符串Str,Str的长度 <= 1000。

Output

输出最少添加多少个字符可以使之变为回文字串。

Input示例

abbc

Output示例

2

思路:把字符串倒过来,看最大公共子序列的长度减去字符串长度即可代码如下:

#include<algorithm>#include<cstdio>#include<cstring>#define max(a, b) (a)>(b)?(a):(b)#define MAXN 1002using namespace std;char s1[MAXN], s2[MAXN];int c[MAXN][MAXN];int len1, len2;void LCSL(){ for (int i = 1; i <= len1; ++i) {  for (int j = 1; j <= len2; ++j)  {   if (s1[i - 1] == s2[j - 1]) c[i][j] = c[i - 1][j - 1] + 1;   else c[i][j] = max(c[i - 1][j], c[i][j - 1]);  } }}int main(){ scanf("%s", s1); len1 = strlen(s1); reverse_copy(s1, s1 + len1, s2); len2 = len1; LCSL(); printf("%d\n", len1 - c[len1][len2]);}

原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9440034.html

时间: 2024-08-13 17:26:49

1092 回文字符串(LCSL_DP)的相关文章

51Nod - 1092 回文字符串

51Nod - 1092 回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba.方案1只需要添加2个字符,是所有方案中添加字符数量最少的. Input 输入一个字符串Str,Str的长度 <= 1000. Output 输出最少添加多少个字符可以使之变为回文字串. Input示例 abbc Output示例 2 题解

1092 回文字符串

1092 回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba.方案1只需要添加2个字符,是所有方案中添加字符数量最少的. Input 输入一个字符串Str,Str的长度 <= 1000. Output 输出最少添加多少个字符可以使之变为回文字串. Input示例 abbc Output示例 2 动态规划,dp[i]

51Nod - 1092 回文字符串(添加删除字符LCS变形)

回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba.方案1只需要添加2个字符,是所有方案中添加字符数量最少的. Input输入一个字符串Str,Str的长度 <= 1000.Output输出最少添加多少个字符可以使之变为回文字串.Sample Input abbc Sample Output 2 向字符串中间添加(

51nod 1092 回文字符串 (dp)

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i][j] 为把以i开头j结尾的子串变为回文串的最少次数, if(s[i]==s[j])  dp[i][j]=dp[i+1][j-1]; else dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1; 1 #include <iostream> 2 #include <

1092 回文字符串(51nod)

原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这题其实可以把字符串str反转一下然后再求两个字符串的最长公共子序列的长度,然后len(str)-那个长度就是答案了= = #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std;

51Nod 1092 回文字符串 | 最长公共子序列变形

求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #define maxn 1005 char s[maxn],s1[maxn]; int dp[maxn][maxn]; int main() { int n=0,i,j,len; scanf("%s",s); len=strlen(s); strcpy(s1,s); strrev(s1); f

判断一个字符串是否为回文字符串

#include <stdio.h> #include <assert.h> #include <string.h> int is_pal_str(const char *p) {  assert(p);  int len = strlen(p);  const char *start = p;  const char *end = p+len - 1;  while (start < end)  {   if (*start == *end)   {    st

【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】

背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该字符串一样的字符串.比如:a,aaaa,aba,abba- 最长回文子串 要求最长回文子串,就须要遍历每个子串,时间复杂度是O(N2):推断字串是不是回文,时间复杂度是O(N),这种话算法的时间复杂度就是O(N3). 我刚開始想到的就是中心扩展法,代码例如以下: public static Stri

判断是否是回文字符串(Java实现)

1.回文的定义:“回文数”就是正读倒读都一样的整数.如奇数个数字:98789,这个数字正读是98789 倒读也是98789.偶数个数字3223也是回文数.字母 abcba 也是回文. 2. 判断一个字符串是否是回文字符串(Java实现) 1 public class Test4 { 2 public static boolean isHuiWen(String text) { 3 int length = text.length(); 4 for (int i = 0; i < length /