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的更多相关文章

  1. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  2. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  3. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  4. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  6. poj 1141 Brackets Sequence (区间dp)

    题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...

  7. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

  8. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  9. POJ 1141 Brackets Sequence(DP)

    题目链接 很早 很早之前就看过的一题,今天终于A了.状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的..二了,直接输出就过了.. #include ...

随机推荐

  1. 229. Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. JS初学之-if else图片顺序及循环切换

    初学JS,代码还需多多改进,自学中... <!doctype html><html><head><meta charset="utf-8" ...

  3. js部分---for循环练习题

    1有一张0.0001米的纸,对折多少次可以达到珠穆朗玛峰的高度8848: <script> /*var h=0.0001; var biao=0; for(;;) { h=h*2; if( ...

  4. Apache脚本路径别名(CGI接口)

    CGI:Common Gateway Interface(通用网关接口)使WEB可以跟一个应用程序进行通信,从通信环境中获得结果. CGI是不安全的,需要mod_alias,mod_cgi模块 Scr ...

  5. Objective-c——UI基础开发第六天(UITableView)

    一.UITableView的简单使用 显示要素: 1.显示多少给区组 2.显示多少行数据 3.每行显示什么内容 代理不会提醒你有什么方法没调用,但是UITableViewDataSource会 1)用 ...

  6. Vue.js相关知识3-路由

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. HDU-4455 Substrings(DP)

    题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...

  8. 【NOIP2013】火柴排队

    如果没有这道题的话我连逆序对是啥都不知道QAQ 原题: 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为: ...

  9. jQuery 1.10.3 参考手册

    Jquery是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及后续版本将不再支持IE6/7 ...

  10. Unity Shaders

    推荐!! Unity技术人员的博客: http://aras-p.info/blog/ Unity 中的 Shader 按照使用方式来分的话,分为3种: Surface Shader 受光照影响 Cg ...