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 ...
随机推荐
- 【ajax】FormData
- 十 SSH
一 Struts 1. 定义:该框架使用 MVC 设计模式开发程序 2. 框架概览: 二 Hibernate 1. 作用:提供了利用面向对象的思想来操作关系型数据的接口 2. 框架图示: 三 Spri ...
- 织梦dedecms模板调用标签大全-提高制作模板速度
关键描述调用标签: ——————————————————————————–模板路径调用标签: {dede:field name=’templeturl’/}—————————————————————— ...
- php 小知识积累
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id']的速度是$row[id]的7倍. 3.echo比print快,并且使用echo的多重 ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- 对石家庄铁道大学官网UI设计的分析
在这一周周一,老师给我们讲了PM,通过对PM的学习,我知道了PM 对项目所有功能的把握, 特别是UI.最差的UI, 体现了团队的组织架构:其次, 体现了产品的内部结构:最好, 体现了用户的自然需求.在 ...
- 批量kill相关所有进程
首先,用ps查看进程,方法如下: $ ps -ef …… smx 1822 1 0 11:38 ? 00:00:49 gnome-terminal smx ...
- codeforces 725/C
Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- POJ1459 Power Network(网络最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total S ...
- 课堂所讲整理:输入输出流(I/O)2(修改版)
package org.hanqi.ex; import java.io.*; public class TestFile2 { public static void main(String[] ar ...