【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这种左右对称的字符串.每个字符 ...
 
随机推荐
- PostgreSQL - 转义字符
			
转载至:postgresql字符转义 前言 在PostgreSQL 9之前的版本中,可以直接使用反斜杠\进行转义:比如:\b表示退格, \n表示换行, \t表示水平制表符,\r标示回车,\f表示换页. ...
 - hbuilder 中文乱码
			
这是因为HBuilder默认文件编码是UTF-8,你可以在工具-选项-常规-工作空间选项中设置默认字符编码
 - C. Tennis Championship dp递推 || 找规律
			
http://codeforces.com/contest/735/problem/C C. Tennis Championship time limit per test 2 seconds mem ...
 - Unity基础知识
			
hierarchy视图选中,点击scene视图,按f键聚焦 persp相当于是透视视野 在persp模式下,物体在scene界面上所呈现的画面是给人一种距离摄像头近的物体显示的大,距离摄像头远的物体显 ...
 - CSS实现文字旋转/实现角标
			
主要用到属性transform:rotate(-30deg) example: .divedittable .project-tag div { width: 43px; line-height: 4 ...
 - hadoop的安装和配置
			
hadoop安装 在Apache Hadoop主页的下载页面https://hadoop.apache.org/releases.html选择版本进行下载: 下载下来的是压缩包: 将压缩包使用Xftp ...
 - Git-远程操作
			
远程分支:远程跟踪分支remote branch是对远程分支状态的引用,是不能移动的,它会根据远程分支变化以及网络通信自动移动.Git服务器包含了远程分支master,在My Computer中的re ...
 - 关于setTimeout和Promise执行顺序问题
			
先看一段代码 console.log('打印'+1); setTimeout(function(){ console.log('打印'+2); }) new Promise(function(reso ...
 - jQuery Validate自定义各种验证方法(转)
			
一.封装自定义验证方法-validate-methods.js /***************************************************************** j ...
 - PMP项目管理学习笔记(8)——整个管理之监控项目工作、综合变更控制、结束项目或阶段
			
监控项目工作 输入:企业环境要素.组织过程资产.项目管理计划.绩效报告 工具:专家判断 输出:变更请求.项目管理计划更新.项目文档更新 综合变更控制 输入:企业环境要素.组织过程资产.项目管理计划.变 ...