思路:

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. AndroidStudio——Android SDK

    前言 安卓的SDK包,跨过长城下载好的,分享出来一下~ Android Studio版本 | 3.4.1 下载地址 微云下载地址 | 链接:https://share.weiyun.com/5rm6l ...

  2. js split分割字符串成数组

    str = "2,2,3,5,6"; //这是一字符串 var strs = new Array(); //定义一数组 strs = str.split("," ...

  3. poj 1789 Truck History 解题报告

    题目链接:http://poj.org/problem?id=1789 题目意思:给出 N 行,每行7个字符你,统计所有的 行 与 行 之间的差值(就是相同位置下字母不相同),一个位置不相同就为1,依 ...

  4. Oracle:spool 的一个用法

    spool 是sqlplus的一个语法,非sql. 平时,我们通过ssh或者xmanger连接到oracle后,如果我们想把我们在上面操作的脚本及脚本执行过程.结果保存下来的话,可以通过spool来实 ...

  5. ASP.NET统计图表控件

    近来客户需要将前段时间开发的统计信息用图表展示出来,还要多个图表类型,例如:柱状图.饼图.曲线图.三维图等等.在网上google了一下,发现了三个(也许更多)可以使用的控件.下面我们一起看看这三个控件 ...

  6. 线段树之成段更新( 需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候)

    HDU  1698 链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直 ...

  7. C++ set和map的简单使用

    C++中的STL模板库的功能可谓相当强大.今天我们来简单说一下set和map的使用方法. 1.pair 我们先来说一下pair.pair定义在头文件<utility>中,其本身相当于一个已 ...

  8. File System Programming --- (二)

    File System Basics The file systems in OS X and iOS handle the persistent storage of data files, app ...

  9. Start Developing Mac Apps -- App Design 应用程序设计

    App Design Apps do not exist on their own. They not only interact seamlessly with their environment, ...

  10. Ubuntu安装eclipse以及创建快捷方式

    1. 安装jdk,我用的1.8,很简单这里不详细说了: 2.下载eclipse的安装包, https://www.eclipse.org/downloads/download.php?file=/te ...