思路:

dp+滚动数组。

实现:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cstring>
  6. using namespace std;
  7.  
  8. int n;
  9. string str;
  10. int dp[][];
  11.  
  12. int solve()
  13. {
  14. for (int i = n - ; i >= ; i--)
  15. {
  16. for (int j = i + ; j < n; j++)
  17. {
  18. if (str[i] == str[j])
  19. {
  20. dp[i & ][j] = dp[(i + ) & ][j - ];
  21. }
  22. else
  23. {
  24. dp[i & ][j] = min(dp[i & ][j - ], dp[(i + ) & ][j]) + ;
  25. }
  26. }
  27. }
  28. return dp[][n - ];
  29. }
  30.  
  31. int solve2()
  32. {
  33. for (int j = ; j <= n; j++)
  34. {
  35. for (int i = ; i <= n - j; i++)
  36. {
  37. if (str[i] == str[i + j - ])
  38. {
  39. dp[i][j % ] = dp[i + ][(j - ) % ];
  40. }
  41. else
  42. {
  43. dp[i][j % ] = min(dp[i][(j - ) % ], dp[i + ][(j - ) % ]) + ;
  44. }
  45. }
  46. }
  47. return dp[][n % ];
  48. }
  49. int main()
  50. {
  51. while (cin >> n >> str)
  52. {
  53. memset(dp, , sizeof(dp));
  54. cout << solve2() << endl;
  55. }
  56. return ;
  57. }

hdu1513 Palindrome的更多相关文章

  1. DP 子序列问题

    函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则返回last的位置举例如下:一个数组number序列 ...

  2. A过的题目

    1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...

  3. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  4. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  9. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. futimens函数的使用【学习笔记】

    #include "apue.h" #include <fcntl.h> int main(int argc,char *argv[]) { int i,fd; str ...

  2. Oracle:通过pl/sql developer工具导入excel数据

    1.在pl/sql developer中选择工具-->ODBC导入器 2.选择需要导入的EXCEL文件(CVS也可以):用户名.口令不用管,直接点“连接”,找到要导入的xls文件 3. 选择“导 ...

  3. SPOJ:Robot(数学期望)

    There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a ...

  4. Struts2的各种标签库

    1 在JSP中使用taglib编译指令导入标签库 <%@ taglib prefix="s" uri="/struts-tags" %> ----- ...

  5. 【USACO】 Balanced Lineup

    [题目链接] 点击打开链接 [算法] 这是一道经典的最值查询(RMQ)问题. 我们首先想到线段树.但有没有更快的方法呢?对于这类问题,我们可以用ST表(稀疏表)算法求解. 稀疏表算法.其实也是一种动态 ...

  6. 关于GitHub的DNS基础设施,你了解吗?

    在 GitHub,我们最近从头改进了 DNS.这包括了我们如何与外部 DNS 提供商交互以及我们如何在内部向我们的主机提供记录.为此,我们必须设计和构建一个新的 DNS 基础设施,它可以随着 GitH ...

  7. Bootstrap-CSS:排版

    ylbtech-Bootstrap-CSS:排版 1.返回顶部 1. Bootstrap 排版 Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans ...

  8. linux中vfork对打开文件的处理

    vfork和fork fork()函数是拷贝一个父进程的副本,拥有独立的代码段 数据段 堆栈空间 然而vfork是共享父亲进程的代码以及代码段 vfork是可以根据需要复制父进程空间,这样很大程度的提 ...

  9. hibernate的基础学习--一对一关联

    一对一关系以丈夫和妻子模型 配置文件 妻子配置文件: <?xml version="1.0" encoding="utf-8" ?> <!DO ...

  10. 从开发的角度对zigbee安全的杂谈

    说起zigbee应该很少人听过,这个B名字怪怪的... 以前开发不懂开发的思想,前前后后花了很久时间,现在回想起来,突然想从安全的角度来理解数据的传输 废话:伴随科技的快速演进,物联网(The Int ...