51Nod 1092 回文字符串(LCS + dp)
数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC。
1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目。(子序列:可以不连续)
2.可以得到公式: ans(最少插入字符)= 字符串总长度 - 最长回文子序列长度
3.如何求最长回文子序列的长度呢? wsx dalao的思路是对区间进行维护(我暂时还写不出来),更一般的做法是动态规划(Dynamic Programming)。
dp 粗略的可以理解为一个状态向另一个状态的转移。一个维度表示一个变量。
仔细想一下的话,很难发现最长回文子序列的长度可以变为求原字符串与其反转字符串最长公共子序列长度(LCS)的题目。
求 LCS 就可以与 dp 联系起来了。
下面列一下状态转移方程:

/**
* Night gathers, and now my watch begins.
* It shall not end until my death.
* I shall take no wife, hold no lands, father no children.
* I shall wear no crowns and win no glory.
* I shall live and die at my post.
* I am the sword in the darkness.
* I am the watcher on the walls.
* I am the fire that burns against the cold,
* the light that wakes the sleepers,
* the shield that guards the realms of men.
* I pledge my life and honor to the Night's Watch,
* for this night,
* and all the nights to come.
*/
#include<bits/stdc++.h>
#define lson i<<2
#define rson i<<2|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define mem(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&-x)
#define pb(x) push_back(x)
#define enld endl
#define mian main
#define itn int
#define prinft printf
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
const double PI = acos (-1.0);
const int INF = 0x3f3f3f3f;
;
;
;
;
using namespace std;
string s; //题目给的原字符串
string sr; //原字符串的翻转字符串
int dp[MAXN][MAXN]; //dp[i+1][j+1]状态表示为 (s0 ~ si) 和 (s0 ~ sj) 的LCS
int main() {
cin >> s;
mem (dp, ); //dp数组初始化
sr = s;
reverse (sr.begin(), sr.end()); //构造翻转串
; i <= s.size(); ++i)
; j <= sr.size(); ++j)
] == sr[j - ])
dp[i][j] = dp[i - ][j - ] + ;
else
dp[i][j] = max (dp[i - ][j], dp[i][j - ]);
cout << s.size() - dp[s.size()][sr.size()] << endl; //ans = s的长度 - LCS(s, sr)
;
}
51Nod 1092 回文字符串(LCS + dp)的更多相关文章
- 51NOD 1092 回文字符串 LCS
Q:给定一个串,问需要插入多少字符才能使其成为回文串,也就是左右对称的串. 经典求LCS题,即最长公共子序列,不用连续的序列.考虑O(n^2^)解法,求LCS起码得有两个串,题中才给了一个串,另一个需 ...
- 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 回文字符串 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...
- 51Nod 1092 回文字符串
最开始毫无头绪,然后参照了一位dalao的博客,思路是一个正序的字符串将其逆序,然后求最长公共子序列(LCS),emm也属于动态规划. #include <iostream> #inclu ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
- 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这种左右对称的字符串.每个字符 ...
随机推荐
- 第一次使用 markdown 写博客
Web前端 js 框架(四选一) 有可能的话,学 Vue.js ,React.js ,Angular.js,Awrelia css 学习 Sass 学会 css 的可编程 HTML5 详细语法 Nod ...
- Docker 入门 第二部分: 容器
目录 Docker 入门 第二部分: 容器 先决条件 介绍 你的新开发环境 使用 Dockerfile 定义一个容器 Dockerfile 应用本身 requirements.txt app.py 构 ...
- Training (deep) Neural Networks Part: 1
Training (deep) Neural Networks Part: 1 Nowadays training deep learning models have become extremely ...
- [软件]Xcode查找系统framework所在路径
有的时候, 我们不小心改了头文件, 导致Xcode系统库被修改(改回去也不行) 假设我改的是UIKit.framework类库里面的一个文件, 那么你只需要从另一个好使的电脑上, 在这个路径找到UIK ...
- ubuntu 开机自动挂载分区
转载: http://blog.sina.com.cn/s/blog_142e95b170102vx2a.html 我的计算机是双硬盘,一个是windows系统,一个是Fedora和ubuntu系统. ...
- C# 解决VS2008在win7找不到输入序列号的地方
1.VS2008在Windows7 打开维护界面看不到可以输序列号的地方. 因为微软把他隐藏了. 2.我们可以借用工具把他显示出来 下载地址:http://www.zlsoft.com/techbbs ...
- 『Matplotlib』数据可视化专项
一.相关知识 官网介绍 matplotlib API 相关博客 matplotlib绘图基础 漂亮插图demo 使用seaborn绘制漂亮的热度图 fig, ax = plt.subplots(2,2 ...
- USB枚举过程【转】
转自:http://blog.csdn.net/myarrow/article/details/8270029 1. 枚举是什么? 枚举就是从设备读取一些信息,知道设备是什么样的设备,如 ...
- nginx_upstream_check_module监控后端服务器http
nginx_upstream_check_module 是专门提供负载均衡器内节点的健康检查的外部模块,由淘宝的姚伟斌大神开发,通过它可以用来检测后端 realserver 的健康状态.如果后端 re ...
- MongoDB aggregate 运用篇(转)
http://www.cnblogs.com/qq78292959/p/4440679.html 最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregat ...