【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. Java数据库连接——jdbc-odbc桥连接方式及汉字乱码问题

    jdbc-odbc桥连接方式操作数据库SU(Course),其中Course属性有Cno,Cname,Cpno,Ccredit. 步骤: 1.配置数据源 控制面板下搜索管理工具->ODBC数据源 ...

  2. ActiveMQ学习笔记(19)----Consumer高级特性(一)

    1. Exclusive Consumer 独有消费者:Queue中的消息是按照顺序被分发到consumer的,然而,当你有多个consumers同时从相同的queue中提取消息时,你将失去这个保证. ...

  3. Unable to load annotation processor factory

    很多人在项目开发中都会遇到项目名称左上角有个红叉,有些是Jar问题,有些是代码问题,有些是编译问题,对于我这种强迫症的是受不了这种情况发生的,如果不影响项目启动还好,废话少说,今天工作就出现了一个问题 ...

  4. 洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom

    https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so exci ...

  5. 数组中出现一次的两个数(三个数)& 求最后一位bit为1

    对于两个数,对于结果中,剩余bit1来异或区分. 下面的解法,非常精简: int lastBitOf1(int number) { ); } void getTwoUnique(vector<i ...

  6. xcode5. 安装cocos2d-x 学习中。。。

    找了一些帖子  没搞出来,后来找到原因了   如今的cocos2d版本号在xcode.5上 没右模版了. 用命令行 来运行.看了官方的文档.最终攻克了--- 对于自己解决的问题都会感到点兴奋. .. ...

  7. html-上左右布局方式---ShinePans

    文件包括 main.html  top.html  left.html  childhood.html  moonsong.html  herethesea.html 主要布局效果: 代码: main ...

  8. struts2 异常页面乱码问题

    在 struts.xml 或者 struts.properties 文件里添加 <constant name="struts.locale" value="zh_C ...

  9. Func委托和Action委托

    http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate The difference between Func a ...

  10. js如何实现简繁体互转

    js如何实现简繁体互转 一.总结 一句话总结:其实无论是简体还是繁体,都是在显示端(前端),其实所有的我只用动js就好了,没必要动php. 当然,后端也可以做前端的事情,只是麻烦了点(要多通信两次,第 ...