描述

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome. 输入

Your program is to read from standard input. The input data has multiple test cases. In each test case, there are two lines. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

输出

Your program is to write to standard output. For each test case, output one line contains one integer, which is the desired minimal number.

样例输入

5

Ab3bd

样例输出

2

题意:给你一串字符串,问你最少需要插入几个字符使得新字符串成为回文字符串;

思路:原字符串p,求得翻转后字符串q,得到p和q的最长公共子串后,最终答案就是p的长度-最长公共子串长度。

注意:因为N较大,开二维数组会mle。所以可以使用滚动数组对LCS的dp数组优化。

参考代码 :

#include <bits/stdc++.h>
#define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define LL long long
#define x first
#define y second
#define PII pair<int,int>
#define PDD pair<double,double>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1e4+5;
const int M=1e7;int dp[2][5555];
int main()
{io;int n;string p,q;while(cin>>n){cin>>p;q=string(p.rbegin(),p.rend());p=" "+p;q=" "+q;int t=0;memset(dp,0,sizeof dp);for(int i=1;i<=n;i++){t=1-t;for(int j=1;j<=n;j++){if(p[i]==q[j]){dp[t][j]=max(dp[t][j],dp[1-t][j-1]+1);}else dp[t][j]=max(dp[1-t][j],dp[t][j-1]);}}cout<<n-dp[t][n]<<"\n";}
}

Palindrome(最长公共子序列 + 滚动数组优化)相关推荐

  1. 求最长公共子序列的空间优化。

    我们在求最长公共子序列时一般方法是c(i,j) = c(i-1,j-1) if s1[i] = s2[j] or max(c[i-1][j],c[i][j-1]) if s1[i] != s2[j]. ...

  2. 【恋上数据结构】动态规划(找零钱、最大连续子序列和、最长上升子序列、最长公共子序列、最长公共子串、0-1背包)

    动态规划(Dynamic Programming) 练习1:找零钱 找零钱 - 暴力递归 找零钱 - 记忆化搜索 找零钱 - 递推 思考题:输出找零钱的具体方案(具体是用了哪些面值的硬币) 找零钱 - ...

  3. 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串 (转)...

    作者:寒小阳 时间:2013年9月. 出处:http://blog.csdn.net/han_xiaoyang/article/details/11969497. 声明:版权所有,转载请注明出处,谢谢 ...

  4. HDU 1513 Palindrome(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个 ...

  5. 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和...

    最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和 文章作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathi ...

  6. 最长公共子序列(O(n)空间优化、O(nlogn)时间优化)

    普通算法 对于求最长公共子序列,最普遍的就是时间复杂度为O(n^2),空间复杂度为O(n*n)算法.重要的是,若要求出最长公共子序列是什么,则必须使用这一种算法,具体代码如下: int dp[maxn ...

  7. Algorithm:C++/python语言实现之求旋转数组最小值、求零子数组、求最长公共子序列和最长公共子串、求LCS与字符串编辑距离

    Algorithm:C++/python语言实现之求旋转数组最小值.求零子数组.求最长公共子序列和最长公共子串.求LCS与字符串编辑距离 目录 一.求旋转数组最小值 1.分析问题 2.解决思路 二.求 ...

  8. 最长重复子数组最长公共子序列不相交的线

    引言 这同样是两种类型的题目,有很多相似的地方和不同的地方,区别依然是连续和不连续之分. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: ...

  9. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列

    题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS   Memory Limit: 65536 ...

最新文章

  1. 2、Eternal框架-svn_有更新!
  2. macbook不能进系统 备份数据_不基于备份和表,生产系统数据误删就能完全恢复?!...
  3. 任意点 曲线距离_中级数学11-曲线函数
  4. java main是标识符吗_main方法的认识 、通配符、java的注释、java的符(标识符)【Java基础】...
  5. 微軟专为Visual Studio 2019设计出一套容器工具擴充套件
  6. python getattr_深入浅出Python模块
  7. SpringCloud工作笔记089---SpringBoot中Mybatis使用Condition_Criteria如何筛选日期类型数据
  8. 对Javascript局部变量的理解
  9. 9 个小技巧让你的 if else 看起来更优雅!
  10. 统计学常见分布、概念
  11. ic卡读卡器软件_读卡器
  12. 华为云盘里面的照片怎么导出来_华为手机误删照片,怎么恢复?别急!只需点击这里...
  13. fiddler4苹果手机证书无法使用的问题解决方案
  14. Linux下C语言编程(1):IO编程
  15. PDF转CAD格式软件下载及使用教程
  16. html隐藏微信举报菜单代码,微信开发-隐藏微信浏览器顶部菜单
  17. Justoj 2388最短区间 贪心
  18. 基于MRCP的FreeSWITCH ASR/TTS开发
  19. 9月6日外盘期货美黄金行情分析\美黄金期货交易策略
  20. linux解压时的tar -zxvf是什么意思?

热门文章

  1. Flowable工作流之加签(委派)、转签(转办)
  2. 【IDEA控制台乱码解决】
  3. matlab微积分计算
  4. jmeter之jtl文件解析(生成测试报告)
  5. P-value个人理解
  6. c语言课程设计-药店管理系统
  7. 医学图像分割评价指标
  8. npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
  9. SpringBoot + Web Socket 实现扫码登录,这种方式太香了!!
  10. 【nexus 私服搭建】