思路:

dp+滚动数组。

实现:

 #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std; int n;
string str;
int dp[][]; int solve()
{
for (int i = n - ; i >= ; i--)
{
for (int j = i + ; j < n; j++)
{
if (str[i] == str[j])
{
dp[i & ][j] = dp[(i + ) & ][j - ];
}
else
{
dp[i & ][j] = min(dp[i & ][j - ], dp[(i + ) & ][j]) + ;
}
}
}
return dp[][n - ];
} int solve2()
{
for (int j = ; j <= n; j++)
{
for (int i = ; i <= n - j; i++)
{
if (str[i] == str[i + j - ])
{
dp[i][j % ] = dp[i + ][(j - ) % ];
}
else
{
dp[i][j % ] = min(dp[i][(j - ) % ], dp[i + ][(j - ) % ]) + ;
}
}
}
return dp[][n % ];
}
int main()
{
while (cin >> n >> str)
{
memset(dp, , sizeof(dp));
cout << solve2() << endl;
}
return ;
}

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

    local_irq_disable 仅仅是 设置 当前CPU 的中断屏蔽位 disable_irq 是禁用 全部cpu 中断(只是当前irq) 如果你要禁止所有的中断该怎么办? 在2.6内核中,可以通 ...

  2. [原创]java获取word里面的文本

    需求场景 开发的web办公系统如果需要处理大量的Word文档(比如有成千上万个文档),用户一定提出查找包含某些关键字的文档的需求,这就要求能够读取 word 中的文字内容,而忽略其中的文字样式.表格. ...

  3. VC++静态连接库

    目录 第1章静态连接库    1 1.1 同名函数的选择    1 1.2 模块合并    2 1.2.1 模块替换    4 1.3 内联函数    4 第1章静态连接库 静态连接库与动态连接库一样 ...

  4. 【矩阵---求A的1到N次幂之和】

    引例: Matrix Power Series: 题目大意,给定矩阵A,求A^+A^+A^+...A^N. 题解:已知X=a,可以通过以下矩阵求出ans=a^+a^+...a^=矩阵^(n+)后右上格 ...

  5. 【转】创建和使用ANDROID LIBRARY工程

    原文网址:http://www.cnblogs.com/Greenwood/archive/2011/06/19/2084499.html 摘要: 创建library供多个工程共享代码.资源是非常常见 ...

  6. Crontab Build_setting的定期检查

    一.脚本功能 (1)检查所有的builting_setting.h是否能够编译通过,并将编译结果写入 编译结果.h文件中. (2)将编译结果通过邮箱发送给相关负责人. (3)系统定期执行任务,检查bu ...

  7. bzoj1097

    最短路+状压dp 肯定是状压dp 那么我们把k个点的单源最短路预处理出来,然后dp[i][j]表示状态为i,当前在j需要走的最短距离,给定的限制用状态压一下就行了 注意特判k=0的情况 #includ ...

  8. 如何 Xcode 开发工具里安装一个空的项目末模板

    很多朋友因为Xcode升级取消了空工程模板而发愁  今天给大家推荐一个简单方便的方法,导入空工程模板 对于 xcode7 来说可以使用下面的方法添加空模板.建议在升级的时候,不要下载beta版,最好下 ...

  9. Get与Post的小知识

    Get与Post的小知识 一.传递参数: Get把参数包含在URL中,而在Post通过request body传递参数.因为参数直接暴露在URL上,GET比POST更不安全,所以不能用来传递敏感信息. ...

  10. innobackupex参数说明

    1.备份: #常用参数 --user:该选项表示备份账号. --password:该选项表示备份的密码. --port:该选项表示备份数据库的端口. --host:该选项表示备份数据库的地址. --s ...