思路:

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. elasticsearch索引查询,日志搜素

    索引查询 http://10.199.137.115:9200/_cat/indices?format=json 返回json字符串的索引状态 增加索引名称过滤 http://10.199.137.1 ...

  2. 隐马尔科夫模型HMM

    崔晓源 翻译 我们通常都习惯寻找一个事物在一段时间里的变化规律.在很多领域我们都希望找到这个规律,比如计算机中的指令顺序,句子中的词顺序和语音中的词顺序等等.一个最适用的例子就是天气的预测. 首先,本 ...

  3. "Activity" 总结

    1.什么是Activity? 1.四大组件之一 2.通常一个界面对应一个activity 3.是Context的子类 4.同时实现window.callback和keyevent.callback回调 ...

  4. Bootstrap-CL:输入框组

    ylbtech-Bootstrap-CL:输入框组 1.返回顶部 1. Bootstrap 输入框组 本章将讲解 Bootstrap 支持的另一个特性,输入框组.输入框组扩展自 表单控件.使用输入框组 ...

  5. 【旧文章搬运】ZwQuerySystemInformation枚举进线程信息

    原文发表于百度空间,2008-10-15========================================================================== 很古老的东 ...

  6. Cmake生成Makefile

    cmake 相比automake 最大的区别是: 步骤没有automake那么多 main.cpp #include<iostream> #include"student.h&q ...

  7. HDU 1713 相遇周期 (最小公倍数)

    题意:... 析:求周期就是这两个分数的最小公倍数,可以先通分,再计算分子的最小倍数. 代码如下: #pragma comment(linker, "/STACK:1024000000,10 ...

  8. 一文教您如何通过 Docker 快速搭建各种测试环境(Mysql, Redis, Elasticsearch, MongoDB) | 建议收藏

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  9. python 之 配置环境变量、通过pip 安装第三方库

    配置环境变量 右击桌面上的“此电脑”—>“属性”—>“高级系统设置”—>右下角“环境变量”—>双击“系统变量”里的“Path”—>点击“新建”—>输入python的 ...

  10. CF1059D Nature Reserve(二分)

    简洁翻译: 有N个点,求与y=0相切的,包含这N个点的最小圆的半径 题解 二分半径右端点开小了结果交了二十几次都没A……mmp…… 考虑一下,显然这个半径是可以二分的 再考虑一下,如果所有点都在y轴同 ...