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 ...
随机推荐
- wddm 部署问题解决
在把wddm部署到一台老的服务上的 windows server 2003 上时遇到了问题,之前也在 windows server 2003 上装过,但是并没有遇到问题,估计和服务器比较老有关系. 问 ...
- url截取判断(实现同级列表)
<script> var dUrl=window.location.href; var cUrl=(dUrl.substring(0, dUrl.indexOf('list_'))); v ...
- debug进入线程中
今天debug调试程序时,怎么也进入不了线程中,f5直接进源码,f6直接跳过了,后来把断点打在线程的Run()方法里面,按f8(可能要多按几次)就可以了.
- Nginx技巧:灵活的server_name,Nginx配置一个服务器多个站点 和 一个站点多个二级域名
http://www.cnblogs.com/buffer/archive/2011/08/17/2143514.html Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活 ...
- CI 框架隐藏index.php-ubuntu
和朋友在做一个小网站,用到了CI框架,之前测试都是在windows上,隐藏index.php也相对比较简单.但服务器是ubuntu系统,需要配置一下,根据网上看到的一些教程,结合自己电脑的特点,记录步 ...
- Sqlserver CheckPoint 在三种恢复模式中的不同表现
准备: 日志截断在下列情况下发生: 1.执行完 BACKUP LOG 语句时.2.在每次处理检查点时(如果数据库使用的是简单恢复模式).这包括 CHECKPOINT 语句所产生的显式检查点和系统生成的 ...
- Queue及Stack
Queue 她是一个接口,有多冢实现方式(LinkedList.ArrayDeque等) 类别 方法 入队 add.offer(优先级高) 出队 remove.poll 查询 element.peek ...
- Spring AOP 实现写事件日志功能
什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...
- API读取和处理的文件
1.FileList对象 FileList对象是File对象的一个集合,设置multiple就可以多文件上传.2.Blob对象 Blob对象就是一个二进制原始数据对象,它提供了slice方法可以读取 ...
- URAL 1320 Graph Decomposition(并查集)
1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...