思路:

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. YTU 2802: 判断字符串是否为回文

    2802: 判断字符串是否为回文 时间限制: 1 Sec  内存限制: 128 MB 提交: 348  解决: 246 题目描述 编写程序,判断输入的一个字符串是否为回文.若是则输出"Yes ...

  2. e.target与e.currentTarget的区别

    在DOM事件对象中有两个属性总是时不时的困扰我,就是target和currentTarget,有时候很迷惑分不清两者的区别,因此有必要把这两个属性好好梳理一下,加深理解,以便日后的查询. MDN中对t ...

  3. Python之如果添加扩展包

    1.首先下载好你需要的扩展包 下载地址是http://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.将你下载好的.whl文件放在你的python文件夹中的Lib\site ...

  4. codeforces 451C. Predict Outcome of the Game 解题报告

    题目链接:http://codeforces.com/problemset/problem/451/C 题目意思:有3支球队(假设编号为1.2.3),总共要打 n 场比赛,已知已经错过这n场比赛中的 ...

  5. 百度地图API应用之获取用户的具体位置

    功能的大概:用户通过点击地图上面的位置,在地图上面进行描点,然后再把获取的到的地理位置保存到地图上面的地址栏目中. 主要是百度地图API的使用 .代码如下: var map = new BMap.Ma ...

  6. View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations

      Animations Animations provide fluid visual transitions between different states of your user inter ...

  7. KDotAlert

    一个iPhone X的适配让楼主受尽了自定义的苦,使用系统API多好,所以在楼主不懈的努力下,终于和组长达成一致:逐步用系统控件替换代码里面的自定义控件,第一个挨刀的就是 BlockAlertsAnd ...

  8. Mac系统下的php扩展开发

    通常在开发PHP的时候,一些核心代码,比如加密函数或需要高效率执行的代码,此时可以用C语言写扩展.本文主要介绍了扩展的开发流程,具体的代码实现参考生成的文件说明. 当前PHP使用的是XAMPP 5.6 ...

  9. 【转】PL/SQL 使用技巧

    ref:http://blog.chinaunix.net/uid-21592001-id-3082675.html [转]plsql developer 使用技巧 Oracle数据库相信已成为很多企 ...

  10. Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)

    传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...