1092 回文字符串

1092 回文字符串

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

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

Input

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

Output

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

Input示例

abbc

Output示例

2

动态规划,dp[i][j]表示str[i...j]最少添加多少个,一开始加在dp[i+1][j-1]的基础上加2了,错了一次发现dp[i][j]的值是min(dp[l][r-1]+1, dp[l+1][r]+1),这样就好写了。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1010;
 4 char str[N];
 5 int dp[N][N];
 6 int main() {
 7     cin >> str+1;
 8     int n = strlen(str+1);
 9     for(int len = 2; len <= n; len ++) {
10         for(int l = 1; l <= n - len + 1; l ++) {
11             int r = l + len - 1;
12             if(str[l] != str[r]) dp[l][r] = min(dp[l][r-1]+1, dp[l+1][r]+1);
13             else dp[l][r] = dp[l+1][r-1];
14         }
15     }
16     printf("%d\n",dp[1][n]);
17     return 0;
18 }
时间: 2024-08-13 17:26:46

1092 回文字符串的相关文章

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 回文字符串(LCSL_DP)

1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba.方案1只需要添加2个字符,是所有方案中添加字符数量最少的. Input 输入一个字符串Str,Str的长度 <= 1000. Output 输出最少添加多少个

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 /