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

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

Input


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

Output


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

Input示例

abbc

Output示例

2

题解


很明显的区间dp

\(dp[l][r]=min(dp[l+1][r],dp[l][r-1])+1\)

\(dp[l][r]=dp[l+1][r-1]\;if(a[l]==a[r])\)

练习了下区间dp的两种写法

记忆化搜索

import java.io.*;
import java.util.*; public class Main {
static final int N=(int)1005;
static int dp[][]=new int[N][N];
static char a[]=new char[N];
static int solve(int l,int r) {
if(l>=r) return 0;
if(dp[l][r]!=-1) return dp[l][r];
int ans=0;
if(a[l]==a[r]) ans=solve(l+1,r-1);
else ans=Math.min(solve(l+1,r),solve(l,r-1))+1;
return dp[l][r]=ans;
}
public static void main(String[] args) {
InputStream sys=System.in;
InputReader in=new InputReader(sys);
PrintWriter out=new PrintWriter(System.out);
a=in.next().toCharArray();
for(int i=0;i<a.length;i++) Arrays.fill(dp[i], -1);
out.println(solve(0,a.length-1));
out.flush();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

二维循环

import java.io.*;
import java.util.*; public class Main {
static final int N=(int)1005;
static int dp[][]=new int[N][N];
static char a[]=new char[N];
public static void main(String[] args) {
InputStream sys=System.in;
InputReader in=new InputReader(sys);
PrintWriter out=new PrintWriter(System.out);
a=in.next().toCharArray();
for(int i=a.length-2;i>=0;i--) {
dp[i][i]=0;
dp[i][i+1]=(a[i]==a[i+1]?0:1);
for(int j=i+2;j<=a.length-1;j++) {
if(a[i]!=a[j])
dp[i][j]=Math.min(dp[i+1][j], dp[i][j-1])+1;
else
dp[i][j]=dp[i+1][j-1];
}
}
out.println(dp[0][a.length-1]);
out.flush();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【51nod 1092】 回文字符串(区间DP)的更多相关文章

  1. 51nod 1092 回文字符串【LCS】

    1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符 ...

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

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

  3. 51Nod 1092 回文字符串(LCS + dp)

    51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...

  4. 51nod 1092 回文字符串 (dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...

  5. 51NOD 1092 回文字符串 LCS

    Q:给定一个串,问需要插入多少字符才能使其成为回文串,也就是左右对称的串. 经典求LCS题,即最长公共子序列,不用连续的序列.考虑O(n^2^)解法,求LCS起码得有两个串,题中才给了一个串,另一个需 ...

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

    求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...

  7. 51Nod 1092 回文字符串

    最开始毫无头绪,然后参照了一位dalao的博客,思路是一个正序的字符串将其逆序,然后求最长公共子序列(LCS),emm也属于动态规划. #include <iostream> #inclu ...

  8. 1042 数字0-9的数量 1050 循环数组最大子段和 1062 序列中最大的数 1067 Bash游戏 V2 1092 回文字符串

    1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 给出一段区间a-b,统计这个区间内0-9出现的次数.   比如 10-19,1出现11次 ...

  9. 51 Nod 1092 回文字符串

    1092 回文字符串  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每 ...

  10. 1092 回文字符串(LCSL_DP)

    1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符 ...

随机推荐

  1. SpringMVC中Freemarker获取项目根目录

    https://blog.csdn.net/whatlookingfor/article/details/51538995 在SpringMVC框架中使用Freemarker试图时,要获取根路径的方式 ...

  2. the little schemer 笔记(0)

    the little schemer 笔记 Z.X.L 2012年08月13日 五项规则 car的规则car只对非空列表有定义. cdr的规则cdr只对非空列表有定义.任何非空列表的cdr是另外一个列 ...

  3. Hibernate Could not obtain transaction-synchronized Session for current thread问题处理

    项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...

  4. Suricata的输出

    不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/output/index.html 总的来说,Suricata采集下来的数据输出分为: ...

  5. SecureCRT中vi或vim编辑器显示中文乱码问题

    vi ~/.vimrc  //新建文件 syntax on set showmode set autowrite set number set encoding=utf-8 fileencodings ...

  6. spring @value 为什么没有获取到值

    1.配置文件的路径没有扫描到 2.注解的bean 不是通过spring托管的.bean 要通过spring 注解,引用的时候要用@Autowired  自动注入的bean 不要用new 出来的bean ...

  7. 使用windows的fsutil命令创建指定大小及类型的测试文件

    在软件测试中,对于上传.下载一类功能常常需要用不同大小的文件进行测试. 使用Windows命令fsutil可以生成任意大小.任意类型文件. C:\Users\axia\fsutil file crea ...

  8. js中的函数编程

    之前在网上看到了一篇教你如何用js写出装逼的代码. 经过学些以及扩展很有收获在这里记录一下. 原文章找不到了.所以就不在这附上链接了. 大家看下下面两段js代码. 上面两端代码效果是一模一样的,都是在 ...

  9. varchar2(100 char)是什么意思

    最佳答案   varchar2(100 char)最长可以插入100个任意字符而varchar2(100)最长可以插入100个英文字符

  10. k8s部署测试实例

    查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...