【UVa】Partitioning by Palindromes(dp)
设w[i,j]为i-j能分割成的最少回文串
f[i]为前i个字符能够分成的最少回文串
w[i,j]=1 当w[i+1,j-1]==1 && s[i]==s[j] 或 i==j-1 && s[i]==s[j]
w[i,j]=w[i+1,j-1]+2 当s[i]!=s[j]
然后
f[i]=min{f[j]+w[j+1,i], 0<=j<i}
f[0]=0
题目白书有
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005;
int n, f[N], w[N][N];
char s[N];
int main() {
int cs=getint();
while(cs--) {
scanf("%s", s+1);
n=strlen(s+1);
CC(f, 0x3f); CC(w, 0); f[0]=0;
for1(i, 1, n) w[i][i]=1;
for1(k, 1, n-1)
for1(i, 1, n-k) {
int j=i+k;
if(k==1 && s[i]==s[j]) w[i][j]=1;
else if(k>1 && w[i+1][j-1]==1 && s[i]==s[j]) w[i][j]=1;
else w[i][j]=w[i+1][j-1]+2;
}
for1(i, 1, n) rep(j, i) f[i]=min(f[i], f[j]+w[j+1][i]);
printf("%d\n", f[n]);
}
return 0;
}
【UVa】Partitioning by Palindromes(dp)的更多相关文章
- 【BZOJ】1068: [SCOI2007]压缩(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...
- UVA 11584 "Partitioning by Palindromes"(DP+Manacher)
传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...
- 【51nod1519】拆方块[Codeforces](dp)
题目传送门:1519 拆方块 首先,我们可以发现,如果第i堆方块被消除,只有三种情况: 1.第i-1堆方块全部被消除: 2.第i+1堆方块全部被消除:(因为两侧的方块能够保护这一堆方块在两侧不暴露) ...
- 【bzoj1925】地精部落[SDOI2010](dp)
题目传送门:1925: [Sdoi2010]地精部落 这道题,,,首先可以一眼看出他是要我们求由1~n的排列组成,并且抖来抖去的序列的方案数.然后再看一眼数据范围,,,似乎是O(n^2)的dp?然后各 ...
- 【ZOJ2278】Fight for Food(dp)
BUPT2017 wintertraining(16) #4 F ZOJ - 2278 题意 给定一个10*10以内的地图,和p(P<=30000)只老鼠,给定其出现位置和时间T(T<=1 ...
- 【POJ】3616 Milking Time(dp)
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10898 Accepted: 4591 Des ...
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- 【vijos】1764 Dual Matrices(dp)
https://vijos.org/p/1764 自从心态好了很多后,做题的确很轻松. 这种题直接考虑我当前拿了一个,剩余空间最大能拿多少即可. 显然我们枚举每一个点拿出一个矩形(这个点作为右下角), ...
- 【Luogu】P3856公共子串(DP)
题目链接 DP.设last[i][j]是第i个串字符'j'所在的最后的位置,f[i][j][k]是第一个串匹配到i,第二个串匹配到j,第三个串匹配到k,最多的公共子串数. 那么我们三重循环i.j.k, ...
随机推荐
- nginx 设置错误的自己定义404页面跳转到500
5年前写的站, 当时是在apache下写的error page, 换了nginx后, 404页面直接跳到了500 server内部错误. 仅仅须要在配置 try_files 的时候指定一下就能够了 我 ...
- 关于图片无缝拼接的学习(PTGui)
一.简介 在用到单反.无人机.手机等拍照工具,需要无缝拼接. 二.下载 官网:http://www.ptgui.com/download.html 其他:http://pan.baidu.com/sh ...
- Unicode 与UTF-8 关系
作者:邱昊宇链接:https://www.zhihu.com/question/23374078/answer/24385963来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 按钮CSS样式
CreateTime--2017年5月5日11:23:18Author:Marydon 按钮CSS样式 实现效果: <input type="button" onclic ...
- LDAP 中 CN,OU,DC 的含意
CN, OU, DC 都是 LDAP 连接服务器的端字符串中的区别名称(DN, Distinguished Name) LDAP连接服务器的连接字串格式为:ldap://servername/DN ...
- 【转帖】Dubbo:来自于阿里巴巴的分布式服务框架
http://www.biaodianfu.com/dubbo.html Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被 ...
- 简单hello world
第一步配置路由: 打开app/http/route.php文件,输入:Route::get('/home', 'HomeController@index'); 第二步配置控制器: 控制文件可以手动添加 ...
- 后台Post/Get 请求接口 方式
Post请求 public string HttpPost(string Url, string postDataStr) { try { HttpWebRequest request = (Http ...
- 转mosquitto auth plugin 编译配置
配置使用 mysql 作为 be (back end) 使用config.mk 配置编译参数 cp config.mk.in config.mk 修改 安装 mysql sudo apt-get in ...
- Android成长之路-手势库的创建
手势库的创建: 使用SDK自带的例子GestureBuilder建立手势库,这个文件夹存在于android\android-sdk-windows \samples\android-10\Gest ...