【Uva 11584】Partitioning by Palindromes
【Link】:https://cn.vjudge.net/contest/170078#problem/G
【Description】
给你若干个只由小写字母组成的字符串;
问你,这个字符串,最少能由多少个回文串组成;
【Solution】
用枚举中心点的方法,得到为回文串的子串;
即bo[1010][1010];
bo[i][j]为true,表示这一段是回文;
否则不是回文;
(回文在枚举的时候,有长度为奇数和长度为偶数两种情况,长度为奇数的,中心点只有一个,长度为偶数的中心点有两个)
然后设dp[i]表示1..i这一段最少需要多少个回文串组成;
则
dp[i]=min(dp[i],dp[j−1]+1);
这里bo[j][i]为true
dp[0]=0
【NumberOf WA】
0
【Reviw】
以为是最长回文子串那题。。
【Code】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
const int INF = 0x3f3f3f3f;
int T,dp[N];
char s[N];
bool is[N][N];
int main(){
//Open();
//Close();
scanf("%d",&T);
while (T--){
ms(dp,INF);
ms(is,0);
scanf("%s",s+1);
int len = strlen(s+1);
rep1(i,1,len){
is[i][i] = true;
int l = i,r = i;
while (l-1 >= 1 && r+1<=len){
l--,r++;
if (s[l]==s[r])
is[l][r] = true;
else
break;
}
l = i,r = i+1;
if (r <= len && s[l]==s[r]){
is[l][r] = true;
while (l-1>=1 && r+1 <= len){
l--,r++;
if (s[l]==s[r])
is[l][r] = true;
else
break;
}
}
}
dp[0] = 0;
rep1(i,1,len){
for (int j = 1;j <= i;j++)
if (is[j][i])
dp[i] = min(dp[i],dp[j-1]+1);
}
cout << dp[len] << endl;
}
return 0;
}
/*
写完之后,明确每一步的作用
*/
【Uva 11584】Partitioning by Palindromes的更多相关文章
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 【UVa 10881】Piotr's Ants
Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...
- 【UVa 116】Unidirectional TSP
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【Uva 11400】Lighting System Design
[Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...
随机推荐
- Hibernate框架学习(二)——api详解
一.Configuration对象 功能:配置加载类,用于加载主配置,orm元数据加载. //1.创建,调用空参构造(还没有读配置文件) Configuration conf=new Configur ...
- [ZJOI2015]诸神眷顾的幻想乡 广义后缀自动机_DFS_语文题
才知道题目中是只有20个叶子节点的意思QAQ.... 这次的广义后缀自动机只是将 last 设为 1, 并重新插入. 相比于正统的写法,比较浪费空间. Code: #include <cstdi ...
- 注解@SuppressWarnings
在JAVA中注解@SuppressWarnings("deprecation")的Deprecation是什么意思 过期的 @SuppressWarnings("depr ...
- python 比较数字大小按从大到小输出
主要用到的python 的知识点 1: 内置函数max 2: 列表的操作 3: while 循环 4 : 错误处理 代码如下: #!/usr/bin/python #coding=u ...
- Vue组件使用基础
这篇博文用来记录 .vue 组件的使用方法. 可以把组件代码按照 template.style.script 的拆分方式,放置到对应的 .vue 文件中. 模板(template).初始数据(data ...
- Node.js使用cnpm
npm是Node.js中维护第三方库.模块的工具,可是国外的速度非常悲剧,这里有一个中国的源cnpm. http://cnpmjs.org/ 须要在命令行中执行 npm install -g cnpm ...
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
1138 - Trailing Zeroes (III) PDF (English) problem=1138" style="color:rgb(79,107,114)" ...
- less07 important
less .foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; font-size: 16px; font-weigh ...
- poj_2352树状数组
因为y已经排好序了,用x坐标建立一维树状数组 #include<iostream> #include<cstdio> #include<cstring> using ...
- BZOJ 3931 Dijkstra+网络流
思路: (我能说按照题意模拟么) 用long long inf 要开大--. //By SiriusRen #include <queue> #include <cstdio> ...