【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.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...
随机推荐
- 51nod 1717 好数 (水题)
题目: 看起来很复杂,其实就是有多少个素因子就翻转多少次. 然后考虑到只有平方数有奇数个素因子. 一次过,上代码把: #include <iostream> #include <al ...
- 【原创】TimeSten安装与配置
1.安装TimeSten 2.安装时要指定TNS_ADMIN_LOCATION,即tnsnames.ora的路径,因为tt会根据这个连接Oracle.C:\TimesTen\tt1122_32\net ...
- Dropout 下(关于《Dropout: A Simple way to prevent neural networks from overfitting》)
先上菜单: 摘要: Deep neural nets with a large number of parameters are very powerful machine learning syst ...
- 前端那些事之----jQuery
1.jquery是什么 一个js的框架,可以方便的使用js 2 什么是jQuery对象 是由jQuery封装后的DOM对象 注意:与DOM对象的方法不同,不可以混用,但是可以相 ...
- How Javascript works (Javascript工作原理) (八) WebAssembly 对比 JavaScript 及其使用场景
个人总结: webworker有以下三种: Dedicated Workers 由主进程实例化并且只能与之进行通信 Shared Workers 可以被运行在同源的所有进程访问(不同的浏览的选项卡,内 ...
- 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)
这道题用到了很多知识点, 是一道好题目. 第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算. 第二这里是隐式 ...
- hdu 1166 敌兵布阵 (线段树单点更新)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) ...
- 数据结构之---C语言实现拓扑排序AOV图
//有向图的拓扑排序 //杨鑫 #include <stdio.h> #include <stdlib.h> #include <string.h> #define ...
- HTML5 canvas炫酷棱镜效果的幻灯片特效
这是一款效果很炫酷华丽的HTML5 canvas带棱镜效果的幻灯片特效. 这个特效在每个幻灯片的前面放置一个图形.并将图形制作为三棱镜效果.它底下的幻灯片图片会被"折射"到棱镜上面 ...
- win7 一个电脑接入多个显示器
Display 在第一个选项display,选中某一个屏幕的时候 如果被选中的屏幕已经是main display,那么下方会显示this is your currently main display ...