【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, ...
随机推荐
- 算法笔记_138:稳定婚姻问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为稳定婚姻问题? 有一个男士的集合Y = {m1,m2,m3...,mn}和一个女士的计划X = {n1,n2,n3,...,nn}.每一个男士有 ...
- ssh登陆不上
用ssh key登陆不上某台机A的某个账号xy1,查看A的/var/log/messages,看到有这么句: User xy1 not allowed because account is locke ...
- 查看IE浏览器安装的插件
请移步至文章:http://blog.sina.com.cn/u/6452627072
- js经常使用功能代码
js经常使用功能代码(持续更新): 1---折叠与展开 <input id="btnDisplay" type="button" class=" ...
- Appium完整安装教程
Appium安装教程 发布时间: 2014-12-11 10:34 作者: 柒月 来源: 51Testing软件测试网采编 字体: 小 中 大 | 上一篇 下一篇 | 打印 | ...
- 系统学习NIO
概述 适用于有一定编程基础的朋友,想系统学习NIO这块知识的朋友.知识点大体分3块:1:>概念了解(各类IO) 2>NIO的核心(缓存区,通道等) 3>网络IO 详细 代码下载:ht ...
- .net core webapi参数绑定处理
在 Startup的ConfigureServices方法中添加: services.Configure<ApiBehaviorOptions>(options => { optio ...
- iOS8的一些控件的变更
UISearchDisplayController变更为UISearchController UIAlertView变更为UIAlertController 如果添加点击事件则需要使用UIAlertC ...
- Memory leak patterns in JavaScript
Handling circular references in JavaScript applications Plugging memory leaks in JavaScript is easy ...
- Junit运行在Spring环境下
@RunWith(SpringJUnit4ClassRunner.class)让测试运行于Spring测试环境 @ContextConfiguration 用来指定加载的Spring配置文件的位置,会 ...