UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)
链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631
题意:
输入一个由小写字母组成的字符串(长度不超过1000),你的任务是把它划分成尽量少的回文串。
例如,racecar本身就是回文串;fastcar只能分成7个单字母的回文串,aaadbccb最少分成3个回文串:aaa, d, bccb。
分析:
设d[i]为字符0~i划分成的最小回文串的个数,则d[i] = min{d[j] + 1 | s[j+1~i]是回文串}。
可以先用O(n*n)时间预处理s[i..j]是否为回文串。方法是枚举中心,然后不断向左右延伸,直到左右字符不同为止。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int UP = + ;
int d[UP]; // d[i]为前i个字符划分成的最小回文串个数
char s[UP];
bool isp[UP][UP]; // isp[i][j]标记s[i..j]是否为回文串 void init(char* s, int len){
memset(isp, false, sizeof(isp));
for(int t = ; t <= len; t++){
for(int f = t, b = t; <= f && b <= len; f--, b++){
if(s[f] == s[b]) isp[f][b] = true;
else break;
}
for(int f = t, b = t + ; <= f && b <= len; f--, b++){
if(s[f] == s[b]) isp[f][b] = true;
else break;
}
}
} int main(){
int T, len;
scanf("%d", &T);
while(T--){
scanf("%s", s + );
init(s, len = strlen(s + ));
for(int t = ; t <= len; t++){
d[t] = t;
for(int i = ; i <= t; i++)
if(isp[i][t]) d[t] = min(d[t], d[i-] + );
}
printf("%d\n", d[len]);
}
return ;
}
UVa 11584 - 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 ...
- UVA 11584 "Partitioning by Palindromes"(DP+Manacher)
传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...
- UVa 11584 Partitioning by Palindromes【DP】
题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...
- UVa 11584 Partitioning by Palindromes (简单DP)
题意:给定一个字符串,求出它最少可分成几个回文串. 析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}. 代码如下: #pra ...
- 区间DP UVA 11584 Partitioning by Palindromes
题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...
- 【线性结构上的动态规划】UVa 11584 - Partitioning by Palindromes
回文串问题.给出一个字符串,问最少可以划分为多少个字符串子串. 对于判断是否为回文串,对于不是很长的字符串,可以采取直接暴力,即从两边向中间收缩判断字符相等. bool is_pali(int l, ...
- UVA 11584 - Partitioning by Palindromes DP
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11584 Partitioning by Palindromes (字符串区间dp)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
问题 怎样找出一个序列中出现次数最多的元素呢? 解决方案 collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案 c ...
- Magento 2中文手册教程 - Magento 2 安装流程图
下图提供了安装Magento 2的安装流程概述: 设置你的服务器环境. 安装magento 2 必备软件, PHP, Apache, MySQL. 系统需求详细信息: 2.1.x 系统需求 获得mag ...
- sql 递归 STUFF
select distinct fm_id, ,,'') AS SO_Nums from [dbo].[t_BADItems] its 表内一对多 的关系查询
- Servlet开发(三)之ServletConfig,ServletContext
1. ServletConfig Servlet是开发动态web的技术,而web.xml是Tomcat工程中最基础也最重要的配置文件,Tomcat启动项目的时候会加载并读取这个文件,其中web.xml ...
- 四 Scatter/Gather
scatter/gather用于描述从Channel中读取或者写入到Channel的操作. 分散(scatter):从Channel中读取在读操作中将读取的数据写入多个Buffer中.因此,Chann ...
- PAT 1082. Read Number in Chinese
#include <cstdio> #include <cstdlib> #include <string> #include <vector> #in ...
- Python入门-装饰器初始
今天我们就围绕一个来展开,那就是:装饰器 一.装饰器 在说装饰器之前,我们先说一个软件设计的原则:开闭原则,又被称为开放封闭原则,你的代码对功能的扩展是开放的,你的程序对修改源代码是封闭的,这样的软件 ...
- Javascript 多物体淡入淡出(透明度变化)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 解决Maven 报 Return code is: 400 , ReasonPhrase:Repository version policy: SNAPSHOT does not allow version: 2.1.0.RELEASE. 的错误
最近在搭建公司的基础框架,业务需求用到elasticsearch,所以需要整合到基础框架里,供各业务线使用同时也便于管理,但在整合的过程中,出现了莫名的问题,同时maven的提示也不够明确. 我的版本 ...
- ERP与电子商务的集成
目前现状: 一般来说,企业中存在三种流:物资流.资金流和信息流,其中,信息流不是孤立存在的,它与物资流和资金流密切相关,反映了物资和资金流动前.流动中和流动后的状况. 电子商务与ERP被分裂开来,没有 ...