G - 回文串

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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.

Input

Your program is to read from standard input. 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.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

  1. 5
  2. Ab3bd

Sample Output

  1. 2
    题目大意:一串字符,从左往右读和从右往左读是完全一样的。
    解题思路:
    这个题目可以转化为求最长公共子串的问题,就是将原字符串逆转,然后求原字符串和逆转字符串的最长公公子串,最后要注意c数组要定义成short型,否则会超内存
  2.  
  3. 程序代码:
  1. #include<cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. #define MAX 5010
  5. char a[MAX],b[MAX];
  6. short c[MAX][MAX];
  7. int max(int x,int y)
  8. {
  9. return x>y?x:y;
  10. }
  11. int main()
  12. {
  13. int n;
  14. scanf("%d",&n);
  15. scanf("%s",a);
  16. for(int i=n-;i>=;i--)
  17. b[n-i-]=a[i];
  18. b[n]='\0';
  19. memset(c,,sizeof(c));
  20. for(int i=;i<n;i++)
  21. for(int j=;j<n;j++)
  22. {
  23. if(a[i]==b[j])
  24. c[i+][j+]=c[i][j]+;
  25. else
  26. {
  27. if(c[i+][j]>c[i][j+])
  28. c[i+][j+]=c[i+][j];
  29. else c[i+][j+]=c[i][j+];
  30. }
  31. }
  32. printf("%d\n",n-c[n][n]);
  33.  
  34. return ;
  35. }

动态规划——G 回文串的更多相关文章

  1. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  2. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  3. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  4. 动态规划——H 最少回文串

    We say a sequence of characters is a palindrome if it is the same written forwards and backwards. Fo ...

  5. UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数

    题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...

  6. 【CPLUSOJ】【动态规划】最短回文串

    题目链接 [问题描述] 如果一个字符串正过来读和倒过来读是一样的,那么这个字符串就被称作回文串.例如abcdcba,abcddbca就是回文串,而abcdabcd不是. 你要解决的问题是:对于任意一个 ...

  7. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  8. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. jquery,javascript -设置某一ul下的li下的 a的属性

    //javascriptvar ul = document.getElementById('ul); var as = ul.getElementsByTagName('a'); for(var i ...

  2. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  3. 在WinForm应用程序中,使用选项卡控件来加载不同的Form界面!

    TabPage tp=new TabPage(); your选项卡控件.Controls.Add(tp); From1 frm=new Form1(); frm.TopLevel = false; f ...

  4. cuda中时间用法

    转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...

  5. 关于Weblogic 10.3.1集群及调优经历

    一.  集群 ·集群易于管理.灵活的负载平衡.较强的安全机制 ·配置前的规划 操作系统 硬件配置 角色 windows IP: 192.168.1.101:7001 AdminServer windo ...

  6. html的input输入框提示信息 点击隐藏

    <input type="text"    <!-- 文本框 --> name="textfield" value="请输入...& ...

  7. 利用set实现去重

    最近读了一些有关于ES6的文章,觉得真是一个超级大的进步,就是不知道兼容性怎么样,鉴于我还在初学,先写个小例子练手,顺便时刻提醒自己要坚持学下去.未来的趋势肯定是替代es5没跑了. var arr=[ ...

  8. sphinx (coreseek)——2、区段查询实例

    首先需要知道区段查询的定义: 索引系统需要通过主查询来获取全部的文档信息,一种简单的实现是将整个表的数据读入内存,但是这可能导致整个表被锁定并使得其他操作被阻止(例如:在MyISAM格式上的INSER ...

  9. RemoteViews嵌入ListView复杂布局

    from http://blog.csdn.net/liliang497/article/details/8308313 主要函数 public void setRemoteAdapter (int ...

  10. 关于sql server 代理(已禁用代理xp)

    由于有数据库在恢复,导致计划不能执行,先操作如下: 关闭数据库的服务..然后把数据库文件剪切出来.然后在起服务.进入SqlSever删除数据库.因为文件已经剪切走了.所以不会删除文件.再把数据库拷到M ...