UVa 11584 Partitioning by Palindromes
来源:互联网 发布:美国恐怖故事 知乎 编辑:程序博客网 时间:2024/05/05 02:37
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631
#include <cstdio>#include <cstring>int n;char str[1010];// d[i]代表将str[0...i]分组的最小组数int d[1010];bool judge(int i, int j);// flag[i][j]为1代表str[i...j]为回文,为0代表不为回文int flag[1010][1010];bool get(int begin, int end);int main(){scanf("%d", &n);int count = 0;while(count < n){memset(str, 0, sizeof(str));memset(flag, -1, sizeof(flag));scanf("%s", str);int len = strlen(str);// 判断回文情况get(0, len-1);/*for(int i = 0; i <= len-1; i++){for(int j = i; j <= len-1; j++)printf("(%d,%d): %d\n", i, j, flag[i][j]);}*/for(int i = 0; i < len; i++){//if(judge(0, i))if(flag[0][i]){//printf("here\n");d[i] = 1;}else{d[i] = i+1;for(int j = 0; j < i; j++){//if(judge(j+1, i))if(flag[j+1][i]){if(d[i] > d[j]+1)d[i] = d[j]+1;}}}//printf("d[%d] = %d\n", i, d[i]);}printf("%d\n", d[len-1]);count++;}return 0;}/*// 判断str[i...j]是否为回文串bool judge(int i, int j){int a;int end = (i+j-1)/2;for(int a = i; a <= end; a++){if(str[a] != str[i+j-a])return false;}return true;}*/// 判断回文情况// 判断str[begin...end]是否为回文bool get(int begin, int end){//printf("here: (%d %d) \n", begin, end);if(flag[begin][end] != -1)return flag[begin][end];if(begin == end){flag[begin][end] = 1;return flag[begin][end];}if(str[begin] == str[end]){if(begin == end-1)flag[begin][end] = 1;elseflag[begin][end] = get(begin+1,end-1);}elseflag[begin][end] = 0;get(begin, end-1);get(begin+1, end);return flag[begin][end];}
该题动态规划较容易想到。其中注意的是,
给定一个字符串str, 得到str[i...j]是否为回文可以动态规划做。
0 0
- Uva 11584 - Partitioning by Palindromes
- UVa 11584 - Partitioning by Palindromes
- Uva-11584-Partitioning by Palindromes
- UVA 11584 - Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA - 11584 Partitioning by Palindromes
- UVa:11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584Partitioning by Palindromes
- UVA 11584 - Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- Uva 11584 Partitioning by Palindromes
- UVa 11584 Partitioning by Palindromes
- UVA - 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- 模拟退火算法
- MySQL索引背后的数据结构及算法原理
- Android自定义属性时TypedArray的使用方法
- wifidog + openwrt相关文章
- java连接数据库工具类写法
- UVa 11584 Partitioning by Palindromes
- java基础_day01_Java 第一次亲密接触
- 手写哈希模板
- 连载《一个程序员的成长历程》-17.最后的项目,得了抑郁症
- Docker新书
- 关于支付宝通知接口中,notify id的时效性
- jQuery 事件与应用
- 二叉搜索树与双向链表
- OCR开源代码以及OCR公开训练测试数据集汇总