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这种左右对称的字符串.每个字符 ...
随机推荐
- bzoj千题计划174:bzoj1800: [Ahoi2009]fly 飞行棋
http://www.lydsy.com/JudgeOnline/problem.php?id=1800 圆上两条直径构成矩形的对角线 #include<cstdio> using nam ...
- Linux命令(七)Linux用户管理和修改文件权限
1. 用户管理 1.1 创建用户/设置密码/删除用户 (-m很重要,自动添加用户家目录) 创建用户组dev, 给用户组dev新建xiaoqin用户,给新用户设置密码! 1.2 查看用户信息 1.3 设 ...
- 略显犀利的 js 判断闰年
/** * 判断闰年函数 * @param {number} year 要判断的年份 * @return {bool} 返回布尔值 * * 其实只要满足下面几个条件即可. * 1.普通年能被4整除且不 ...
- html5 canvas创建阴影
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 第11月第3天 直播 rtmp yuv
1. LiveVideoCoreSDK AudioUnitRender ==> MicSource::inputCallback ==> GenericAudioMixer::pushBu ...
- [译]How To Use the Linux Auditing System on CentOS 7
本文是How To Use the Linux Auditing System on CentOS 7的中文版,翻译不到之处,还请指出和多多包涵.本文并不会完全遵从原文的一些格式,而是加入自己学习的理 ...
- webpack react 错误整理
1.ERROR in ./src/entry.js Module build failed: SyntaxError 解决方法: 安装babel-preset-react, npm install ...
- Dream------Hadoop--网络拓扑与Hadoop--摘抄
两个节点在一个本地网络中被称为“彼此的近邻”是什么意思?在高容量数据处理中,限制因素是我们在节点间 传送数据的速率-----带宽很稀缺.这个想法便是将两个节点间的带宽作为距离的衡量标准. 衡量节点 ...
- 2017/05/23 java 基础 随笔
1.多态的好处: a.提高了代码的维护性(继承保证) b.提高了代码的扩展性(由多态保证) package com.huawei; public class Demo2 { public static ...
- 003_cd pushd popd三个命令的区别
一. It depends. In zsh you can configure cd to push the old directory on the directory stack automati ...