UVa11584 - Partitioning by Palindromes(区间DP)
题目大意
给定一个小写字母组成的字符串S,你的任务是划分成尽量少的回文串
题解
方程就是dp[j]=min(dp[i-1]+1)(i<=j,s[i..j]是回文串)
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 1005
char s[MAXN];
int dp[MAXN];
bool check(int l,int r)
{
int len=(l+r)/2;
for(int i=l; i<=len; i++)
if(s[i]!=s[r-i+l]) return false;
return true;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=1; i<=len+1; i++)
dp[i]=1005;
dp[0]=0;
for(int i=1; i<=len; i++)
{
for(int j=1; j<=i; j++)
if(check(j,i))
dp[i]=min(dp[i],dp[j-1]+1);
}
printf("%d\n",dp[len]);
}
return 0;
}
UVa11584 - Partitioning by Palindromes(区间DP)的更多相关文章
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[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 ...
- codeforces 245H . Queries for Number of Palindromes 区间dp
题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...
- UVA-11584 Partitioning by Palindromes (简单线性DP)
题目大意:给一个全是小写字母的字符串,判断最少可分为几个回文子序列.如:“aaadbccb” 最少能分为 “aaa” “d” “bccb” 共三个回文子序列,又如 “aaa” 最少能分为 1 个回文子 ...
- K - Queries for Number of Palindromes(区间dp+容斥)
You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There a ...
- 【题解】UVA11584 Partitioning by Palindromes
UVA11584 https://www.luogu.org/problemnew/show/UVA11584 暑假开始刷lrj紫/蓝书DP题 这几天做的一道 思路 预处理出所有的回文串是否存在 前提 ...
- UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数
题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...
- UVa 11584 Partitioning by Palindromes【DP】
题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...
- Codeforces245H - Queries for Number of Palindromes(区间DP)
题目大意 给定一个字符串s,q个查询,每次查询返回s[l-r]含有的回文子串个数(题目地址) 题解 和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数 用dp[i][j]表示,s ...
随机推荐
- 泛型转Datatable
//自定义扩展方法 public static class ExtMethod { //泛型转为DataTable public static DataTable AsDataTable<T&g ...
- codeforces 633D - Fibonacci-ish 离散化 + 二分查询
Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...
- 成为JavaGC专家(2)—如何监控Java垃圾回收机制
什么是GC监控? 垃圾回收收集监控指的是搞清楚JVM如何执行GC的过程,例如,我们可以查明: 1. 何时一个新生代中的对象被移动到老年代时,所花费的时间. 2. Stop-t ...
- UGUI-组件
2015-06-22 UGUI 组件 Canvas 画布 The Canvas component represents the abstract space in which the UI is l ...
- 使用ssh公钥实现免密码登录
使用ssh公钥实现免密码登录 ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(10.207.160.34),B(10 ...
- tomcat+apache 实现负载均衡之一:同一台电脑部署2个以上tomcat
1. 下载tomcat 8.0.17 http://apache.fayea.com/tomcat/tomcat-8/v8.0.17/bin/apache-tomcat-8.0.17.tar.gz ...
- 关于size_t与size_type
整理自关于size_t与size_type 问题起源于这样一段代码: #include <algorithm> #include <stdio.h> int main() { ...
- 【NOIP 2013 DAY2 T3】 华容道(spfa)
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- QT 的信号与槽机制介绍
https://www.ibm.com/developerworks/cn/linux/guitoolkit/qt/signal-slot/ http://www.kuqin.com/qtdocume ...
- 自定义NavigationView's item 的高度
http://stackoverflow.com/questions/31204320/how-can-i-change-the-navigationviews-item-text-size 自定义s ...