hdu1513 Palindrome
思路:
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的更多相关文章
- DP 子序列问题
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则返回last的位置举例如下:一个数组number序列 ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [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 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- futimens函数的使用【学习笔记】
#include "apue.h" #include <fcntl.h> int main(int argc,char *argv[]) { int i,fd; str ...
- Oracle:通过pl/sql developer工具导入excel数据
1.在pl/sql developer中选择工具-->ODBC导入器 2.选择需要导入的EXCEL文件(CVS也可以):用户名.口令不用管,直接点“连接”,找到要导入的xls文件 3. 选择“导 ...
- SPOJ:Robot(数学期望)
There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a ...
- Struts2的各种标签库
1 在JSP中使用taglib编译指令导入标签库 <%@ taglib prefix="s" uri="/struts-tags" %> ----- ...
- 【USACO】 Balanced Lineup
[题目链接] 点击打开链接 [算法] 这是一道经典的最值查询(RMQ)问题. 我们首先想到线段树.但有没有更快的方法呢?对于这类问题,我们可以用ST表(稀疏表)算法求解. 稀疏表算法.其实也是一种动态 ...
- 关于GitHub的DNS基础设施,你了解吗?
在 GitHub,我们最近从头改进了 DNS.这包括了我们如何与外部 DNS 提供商交互以及我们如何在内部向我们的主机提供记录.为此,我们必须设计和构建一个新的 DNS 基础设施,它可以随着 GitH ...
- Bootstrap-CSS:排版
ylbtech-Bootstrap-CSS:排版 1.返回顶部 1. Bootstrap 排版 Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans ...
- linux中vfork对打开文件的处理
vfork和fork fork()函数是拷贝一个父进程的副本,拥有独立的代码段 数据段 堆栈空间 然而vfork是共享父亲进程的代码以及代码段 vfork是可以根据需要复制父进程空间,这样很大程度的提 ...
- hibernate的基础学习--一对一关联
一对一关系以丈夫和妻子模型 配置文件 妻子配置文件: <?xml version="1.0" encoding="utf-8" ?> <!DO ...
- 从开发的角度对zigbee安全的杂谈
说起zigbee应该很少人听过,这个B名字怪怪的... 以前开发不懂开发的思想,前前后后花了很久时间,现在回想起来,突然想从安全的角度来理解数据的传输 废话:伴随科技的快速演进,物联网(The Int ...