51Nod 1092

数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了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)的更多相关文章

  1. 51NOD 1092 回文字符串 LCS

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

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

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

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

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

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

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

  5. 51Nod 1092 回文字符串

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

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

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

  7. 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次 ...

  8. 51 Nod 1092 回文字符串

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

  9. 1092 回文字符串(LCSL_DP)

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

随机推荐

  1. mysql出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' 错误

    init 神奇..其他的都没有成功,这个居然成功了!! 还试验过:sudo mysqld restart啥的,都没有用......

  2. spring框架学习(一)入门

    spring 入门--IOC  1.导入jar包 4 + 1  : 4个核心(beans.core.context.expression) + 1个依赖(commons-loggins...jar) ...

  3. svn 更新代码

    SVN 更新代码 --force # 强制覆盖 /usr/bin/svn --username user --password passwd co $Code ${SvnPath}src/ # 检出整 ...

  4. Oracle 基本操作符

    1.一般操作符 (1)!= 不等于 select empno,ename,job from scott.emp where job!='manager' (2)^= 不等于 select empno, ...

  5. mybatis批量增加与删除——(十五)

    1.首先应该明白,mybatis增删改返回值是int型的影响行数的值 mapper接口 package cn.xm.mapper; import java.util.List; import cn.x ...

  6. 总结WCF开发中遇到的几个问题

    最近的项目,需要用到WCF,在以前的工作中,经常是将WCF托管在IIS中,主要有几下几个原因:      第一:部署非常方便,和部署一个站点没什么区别:      第二:不受防火墙的影响,因为一般服务 ...

  7. asp.net动态增加服务器端控件并提交表单

    为什么要用原生的呢? 1.目的 原生出现浏览器兼容性问题 极少,不用测试多浏览兼容性 .需要考虑到市面上的其他垃圾浏览器. 2.性能不好 如果不考虑第一条 你可以换一种方式 直接上代码 .aspx页面 ...

  8. C/C++杂记:深入理解数据成员指针、函数成员指针

    1. 数据成员指针 对于普通指针变量来说,其值是它所指向的地址,0表示空指针. 而对于数据成员指针变量来说,其值是数据成员所在地址相对于对象起始地址的偏移值,空指针用-1表示.例: 代码示例: str ...

  9. vue里面使用Velocity.js

    英文文档:http://velocityjs.org/ https://github.com/julianshapiro/velocity 中文手册(教程):http://www.mrfront.co ...

  10. express中间件代理实现跨域

    前端代码 var xhr = new XMLHttpRequest(); xhr.open('post', 'http://localhost:3000', true); xhr.onreadysta ...