uva 11584 - 字符串 dp
一个长度1000的字符串最少划分为几个回文字符串
-----------------------------------------------------------------------------------------------------------------
想复杂了。
首先N2的时间预处理一下,从i开始长度为len的字符串是否为回文串。
dist(i) = MIN(dist(i),dist(j)+1) 如果 j-i 为一个回文串
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
using namespace std;
typedef long long LL;
const int N = ;
bool dp[N][N];
int minDist[N];
char str[N];
int main(){
int n; for(cin>>n;n--;){
scanf("%s",str); int length = strlen(str); for(int i=;i<length;i++) minDist[i] = i+; memset(dp,false,sizeof(dp));
for(int len=;len<;len++) for(int i=;i<length;i++) {
dp[len][i]=true;
}
for(int len=;len<=length;len++){
for(int i=;i<length;i++){
if(len+i>length) break;
dp[len][i] = (str[i]==str[i+len-])&&(dp[len-][i+]);
}
}
for(int i=;i<length;i++){
for(int j=;j<=i;j++){
if(dp[i-j+][j]) minDist[i] = MIN(minDist[i],j?(minDist[j-]+):);
}
}
printf("%d\n",minDist[length-]);
}
return ;
} /*
3
racecar
fastcar
aaadbccb
*/
uva 11584 - 字符串 dp的更多相关文章
- Partitioning by Palindromes UVA - 11584 简单dp
题目:题目链接 思路:预处理出l到r为回文串的子串,然后如果j到i为回文串,dp[i] = min(dp[i], dp[j] + 1) AC代码: #include <iostream> ...
- UVA 11584 入门DP
一开始把它当成暴力来做了,即,从终点开始,枚举其最长的回文串,一旦是最长的,马上就ans++,再计算另外的部分...结果WA了 事实证明就是一个简单DP,算出两个两个点组成的线段是否为回文,再用LCS ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- 区间DP UVA 11584 Partitioning by Palindromes
题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- 【BZOJ 2121】 (字符串DP,区间DP)
2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...
随机推荐
- python 一句话输出26个英文字母
chr(i) # return i character ord(c) # return integer >>> [chr(i) for i in range(97,123)] ['a ...
- 产品开发也要看阵容,APP开发只需五步变得靠谱
最早认识的一个朋友是程序员,曾经到一家外包公司接单子,小外包公司经常遇到的问题就是和需求方谈产品功能.客户要做外包,对方让他一次性报价,但是客户连功能点自己都不清楚,这时朋友说还是按照具体功能点来做吧 ...
- Unity 获取坐标函数 坐标转换函数
获取世界坐标:transform.position 获取本地坐标:transform.localPosition 获取鼠标坐标:Input.mousePosition 获取手指触摸区域坐标:Input ...
- 安装Oracle 12c及解决遇到的问题
一.[INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置) 原文链接:https://blog.csdn.net/u013388049/article/details/85 ...
- 【技术翻译】SIFT算子原理及其实现 (一)介绍
介绍 匹配不同图片的特征是计算机视觉常见的问题. 当所有要匹配的图片很相似的时候(大小,方位),简单的角点检测算子就可以匹配,但是,当你的图片大小,方位不同的时候,你就要用到尺度不变特征变换(scal ...
- ansible 连通测试
[root@ftp:/root] > ansible ansible01 -m ping ansible01 | UNREACHABLE! => { "changed" ...
- 我的Linux系统开始学习的过程
Linux系统,不知大家是否了解.接触计算机不多或对计算机不感冒的人可能对其比较陌生,曾经的我也是.上大学前的我的确对Linux一无所知,那时候接触面窄,都没有听说过此名字,上了大学后,身边的人有学习 ...
- pytorch 6 build_nn_quickly 快速搭建神经网络
import torch import torch.nn.functional as F # replace following class code with an easy sequential ...
- django-4-模板标签,模板继承
<<<模板标签>>> {% for %}{% endfor %} 循环 {% if %}{% elif %}{% else %}{% endif %} 判断 {% ...
- mysql存储小数
线下不知道什么版本的古董了,经本人亲测,varchar类型的数据,可以直接执行mysql的sum函数. ________________________________________________ ...