POJ #1141 - Brackets Sequence - TODO: POJ website issue
A bottom-up DP. To be honest, it is not easy to relate DP to this problem. Maybe, all "most"\"least" problems can be solved using DP..
Reference: http://blog.sina.com.cn/s/blog_8e6023de01014ptz.html
There's an important details to AC: in case of "())", there are 2 solutions: ()() and (()). For the AC code, the former one is preferred.
// 1141
// http://blog.sina.com.cn/s/blog_8e6023de01014ptz.html
//
#include <stdio.h>
#include <string.h>
#include <memory.h> #define MAX_LEN 101 bool isMatched(char *in, int i, int j)
{
return (in[i] == '(' && in[j] == ')') || (in[i] == '[' && in[j] == ']');
}
void printPath(int path[MAX_LEN][MAX_LEN], int i, int j, char in[MAX_LEN])
{
int sInx = path[i][j];
if (sInx == -)
{
if (i == j)
{
//printf("Complete @ %d\n", i);
switch (in[i])
{
case '(':
case ')': printf("()"); break;
case '[':
case ']': printf("[]"); break;
}
return;
}
else if (i + == j)
{
//printf("Already matched: [%d, %d]\n", i, j);
printf("%c%c", in[i], in[j]);
return;
}
else if ((i+) < j)
{
printf("%c", in[i]);
printPath(path, i + , j - , in);
printf("%c", in[j]);
}
}
else
{
printPath(path, , path[i][j], in);
//printf("Break @ %d\n", path[i][j], in);
printPath(path, path[i][j] + , j, in);
}
} void calc(char in[MAX_LEN])
{
unsigned slen = strlen(in);
if (slen == )
{
printf("\n");
return;
}
else if (slen > )
{
int dp[MAX_LEN][MAX_LEN];
int path[MAX_LEN][MAX_LEN]; // Init
for (int i = ; i < MAX_LEN; i ++)
for (int j = ; j < MAX_LEN; j++)
{
dp[i][j] = 0xFFFFFF;
path[i][j] = -;
} // Def: dp[i][j] = min num of chars to add, from i to j
// Recurrence relation:
// 1. dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]), where k in (i..j)
// 2. dp[i][j] = dp[i+1][j-1], <IF in[i]:in[j] = [] or ()> // i..j is range, k is interval
for (int k = ; k < slen; k++) // NOTE: this is a bottom-up DP. We have to go from 0 to slen as interval
for (int i = , j = i + k; i < slen - k; i ++, j ++)
{
if (i == j)
{
dp[i][j] = ;
path[i][j] = -;
continue;
}
bool bIsMatched = isMatched(in, i, j);
if (bIsMatched) // eq 2
{
if (k == ) // length == 2
{
dp[i][j] = ;
path[i][j] = -;
continue;
}
else if (k > ) // length > 2
{
dp[i][j] = dp[i + ][j - ];
path[i][j] = -; // we don't split matched pair
// A: we still go ahead with eq1
}
}
//else // eq1
{
// t is iterator of split index
for (int t = i; t < j; t++)
{
int newVal = dp[i][t] + dp[t + ][j];
if (newVal <= dp[i][j]) // Label A: we prefer splitted solution
{
dp[i][j] = newVal;
path[i][j] = t;
}
}
}
}
printPath(path, , slen - , in);
} // if (slen > 0)
} int main()
{
char in[MAX_LEN] = { };
gets(in);
calc(in);
printf("\n");
return ;
}
POJ #1141 - Brackets Sequence - TODO: POJ website issue的更多相关文章
- 区间DP POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29520 Accepted: 840 ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- poj 1141 Brackets Sequence(区间DP)
题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
- POJ 1141 Brackets Sequence(DP)
题目链接 很早 很早之前就看过的一题,今天终于A了.状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的..二了,直接输出就过了.. #include ...
随机推荐
- PostgreSQL增删数据命令示例
在PostgreSQL中如何用简单的几条SQL语句生成大量的测试数据呢? 此处,我简单的写一个例子,供参考(在Postgresql9.1下面做的): (1)生成一万条测试数据的表foo mydb=# ...
- (基础篇) php中0与空 Null false的区别
<?php $test=0; if($test==''){ echo '<br />在php中,0即为空'; //被输出 } if($test===''){ echo '<br ...
- Python 将pdf转换成txt(不处理图片)
上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要.查找了很多资料,在linux下要将d ...
- NAT(未验证,后续见Linux服务器架构篇)
通常小型企业或是学校单位大多数仅有一条对外的联机,然后全公司内的计算机全部通过这条联机连到因特网上,此时我们更需使用IP分享器来让这一条对外联机分享给所有公司内部员工使用,那么Linux能不能达到此一 ...
- HDU 2089 数位dp/字符串处理 两种方法
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- C#部分---语言经典题目——兔子生兔子
根据本月成兔=上月成兔+上月小兔:本月小兔=上月幼兔:本月幼兔=本月成兔 利用while循环: Console.WriteLine("请输入月份:"); //int m = int ...
- android中常见对话框之一AlertDialog
在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...
- POJ-1155 TELE (树形DP+分组背包)
题目大意:给一棵带边权的有根树,每个叶子节点有权.边权表示代价,叶子节点的权值代表可以补偿多少代价.问从根节点最多可以到达多少个叶子,使得付出的总代价不大于0. 题目分析:定义状态dp(u,k)表示从 ...
- PPT图片快速编辑技巧
修改*.ppt或*.pptx的后缀名为zip 例如:demo.pptx修改为demo.zip 修改为, 使用压缩软件打开此压缩包, 一般图片资源都会存放在ppt/media下 找到你所要修改的图片,然 ...
- 黑马程序员——JAVA基础之抽象和接口 , 模版方法设计模式
------- android培训.java培训.期待与您交流! ---------- 抽象定义: 抽象就是从多个事物中将共性的,本质的内容抽取出来. 例如:狼 ...