UVa 11584 Partitioning by Palindromes (简单DP)
题意:给定一个字符串,求出它最少可分成几个回文串。
析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #include <unordered_map>
- #include <unordered_set>
- #define debug() puts("++++");
- #define freopenr freopen("in.txt", "r", stdin)
- #define freopenw freopen("out.txt", "w", stdout)
- using namespace std;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f3f3f;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 1e3 + 5;
- const int mod = 2000;
- const int dr[] = {-1, 1, 0, 0};
- const int dc[] = {0, 0, 1, -1};
- const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- char s[maxn];
- int dp[maxn];
- bool judge(int i, int j){
- while(i < j){
- if(s[i] != s[j]) return false;
- ++i, --j;
- }
- return true;
- }
- int main(){
- int T; cin >> T;
- while(T--){
- scanf("%s", s);
- n = strlen(s);
- memset(dp, INF, sizeof dp);
- dp[0] = 1;
- for(int i = 1; i < n; ++i){
- for(int j = 0; j < i; ++j)
- if(judge(j, i)) dp[i] = min(dp[i], 1 + dp[j-1]);
- else dp[i] = min(dp[i], dp[i-1]+1);
- }
- printf("%d\n", dp[n-1]);
- }
- return 0;
- }
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】
题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...
- UVA 11584 "Partitioning by Palindromes"(DP+Manacher)
传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...
- 区间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
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 ...
- UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)
题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...
随机推荐
- BUPT复试专题—科学计算器(2009)
题目描述 给你一个不带括号的表达式,这个表达式只包含加.减.乘.除,请求出这个表 达式的最后结果,最后结果一定是整数: 输入 一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符 " ...
- TCP/IP Protocol Architecture
原文: https://technet.microsoft.com/en-sg/library/cc958821.aspx 1. 主机到网络层 2.网络互连层(互连这个翻译好) ----------- ...
- 出现异常时直接把e输出比输出e.getMessage()好得多
之前研究态度不好,出异常时处理草草了事,今天出现问题才觉得该认真对待每个分支.
- SolidEdge如何复制特征 建立类似于UG 块的概念
直接Ctrl+C和Ctrl+V可以实现特征的复制粘贴 按N键可以改变特征方向 已经复制完成的特征要进行定位,则右击该特征,编辑轮廓,可以进行聪慧尺寸的标注 使用特征库的方式,就像UG的块一样, ...
- bsp开发之驱动开发
驱动程序是可以管理虚拟设备或者物理设备,协议,服务等得软件模块,操作系统仅仅有通过驱动程序才干訪问硬件.针对windows ce开发设备驱动.就是通过platform builder创建一个新的平台, ...
- HBase技术简介
一.HBase简介 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. ...
- php闭包实例
php闭包函数,一个典型的实例 function getMoney() { $rmb = 1; $dollar = 6; $func = function($dollar) use (&$rm ...
- HTTP的上传文件实例分析
这个是http文件传输的一种格式,当时不知道这种格式,废弃. HTTP的上传文件实例分析 由于论坛不支持Word写文章发帖. 首先就是附件发送怎么搞,这个必须解决.论坛是php的.我用Chrome类浏 ...
- Devices下设备的进程显示为问号的问题
adb shell getprop ro.debuggable 返回 ro.debuggable=0 说明这个机子的版本不是userdebug版本,是user版本 只有这 ...
- Json的简单介绍和解析
Json:JavaScript对象表示法(JavaScript Object Noatation) Json是存储和交换文本信息的语法,类似XML.它采用键值对的方式来组织,易于人们阅读和编写,同时也 ...