【51nod 1092】 回文字符串(区间DP)
回文串是指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)的更多相关文章
- 51nod 1092 回文字符串【LCS】
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符 ...
- 51Nod - 1092 回文字符串(添加删除字符LCS变形)
回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca, ...
- 51Nod 1092 回文字符串(LCS + dp)
51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...
- 51nod 1092 回文字符串 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...
- 51NOD 1092 回文字符串 LCS
Q:给定一个串,问需要插入多少字符才能使其成为回文串,也就是左右对称的串. 经典求LCS题,即最长公共子序列,不用连续的序列.考虑O(n^2^)解法,求LCS起码得有两个串,题中才给了一个串,另一个需 ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
- 51Nod 1092 回文字符串
最开始毫无头绪,然后参照了一位dalao的博客,思路是一个正序的字符串将其逆序,然后求最长公共子序列(LCS),emm也属于动态规划. #include <iostream> #inclu ...
- 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次 ...
- 51 Nod 1092 回文字符串
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每 ...
- 1092 回文字符串(LCSL_DP)
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符 ...
随机推荐
- hdu 2189 悼念512汶川大地震遇难同胞——来生一起走 基础母函数
#include <iostream> #include <algorithm> #include <cstring> using namespace std; ] ...
- Python实现两已知排好序的列表合并成一个排好序的列表
#方法0.5--- lst1 = [1, 3, 7, 9, 12] lst2 = [4, 8, 9, 13, 15, 19] def merge(a, b): c = [] h = j = 0 whi ...
- ssh配置详解及公私钥批量分发
第一:ssh配置文件详解 第二:ssh公私密钥的生成 第三:ssh公钥分发之一:ssh自带工具ssh-copy-id工具分发 第四:ssh公钥分发之二:编写sshpass脚本批量分发 第五:ssh公钥 ...
- ubuntu14.04 在Dash中添加条目并把它放到启动器上
1. 创建studio.desktop,内容如下:注意路径. [Desktop Entry] Version=2.2.3 Name=Android Studio Exec=/home/你的用户名/an ...
- DataTable数据检索的性能分析[转]
原文链接 作者写得非常好,我学到了许多东西,这里只是转载! 我们知道在.NET平台上有很多种数据存储,检索解决方案-ADO.NET Entity Framework,ASP.NET Dynamic D ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- LVS实现负载均衡
三台主机模拟 sishen_63(分发器): eth0(Bridge):192.168.1.63 eth1(vmnet4):192.168.2.63 sishen_64(RealServer1): e ...
- dangerouslySetHTML 和 style 属性
这一节我们来补充两个之前没有提到的属性,但是在 React.js 组件开发中也非常常用,但是它们也很简单. dangerouslySetHTML 出于安全考虑的原因(XSS 攻击),在 React.j ...
- 使用原生javascript实现jquery的$(function(){ })
在使用jquery的时候,经常用到$(function(){})方法或者是$(document).read(function(){})来作为页面dom节点加载完成之后javascript的执行入口,现 ...