【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的更多相关文章

  1. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  2. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  3. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  4. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  5. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. 【Uva 11400】Lighting System Design

    [Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...

随机推荐

  1. CSS3———linear-gradient() 线性渐变

    线性渐变linear-gradient() 遇到了这样的css样式 body { height: 100%; background-color: #ffffff; background-image: ...

  2. 路飞学城Python-Day14

    转载:python之路-路飞学城-python-book [25.常用模块-logging模块详解] [26.常用模块-logging模块详解2] [27.常用模块-logging模块日志过滤和日志文 ...

  3. java jar打包命令使用

    用法:jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目录] 文件名 ... 选项: -c 创建新的存档 -t 列出存档内容的列表 -x 展开存档中的命名的 ...

  4. vue-cli解析

    前言 这段时间,算是空出手来写几篇文章了.由于很久都没有时间整理现在所用的东西了,所以,接下来会慢慢整理出一些文档来记录前段时间的工作和生活. 这篇文章的主题是vue-cli的理解.或许,很多人在开发 ...

  5. Qt之pro配置多个子工程/子模块

    简述 进行Qt项目开发的时候,尤其是大型项目,经常涉及多工程/多模块问题,其主要思想还是模块化,目的是为了降低程序复杂度,使程序设计.调试和维护等操作简单化. 简述 配置 效果 多工程 多模块 更多参 ...

  6. js时间格式化函数,支持Unix时间戳

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. Unity游戏开发--30s制作精美地图

    "君子生非异也.善假于物也"--<劝学>荀子 引用这句话的目的,是我觉得有时候.利用工具来提高游戏开发效率是很必要的. 利用工具,解放程序员双手. 今天想给大家介绍下. ...

  8. Majority Element:主元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. Mvc前后端显示不同的404错误页

    最近做的系统前端是移动端的,后端是PC端,然后404页面不能用通一个,so  查找了一些资料,找到了一个解决办法 在Global.asax文件夹下添加Application_EndRequest事件处 ...

  10. 基于JS的身份证验证(完整版)

    var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]; // 加权因子 var ValideCode = [ 1, 0 ...