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. Cache

    1.Cache中的块与主存储器中的块时按照什么样的规则建立对应关系的? 2.在这种对应关系下,主存地址又是如何变换成Cache地址的? Cache信息: 1.数据Cache和指令Cache是分开还是统 ...

  2. (进阶篇)浅谈COOKIE和SESSION关系和区别

    COOKIE介绍 cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,您能够创建并取回 coo ...

  3. socket 简介

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1.         什么是TCP/IP.UDP?2.         Sock ...

  4. hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. squid代理服务器搭建及配置

    系统环境:CentOS release 6.5 (Final)(最小化安装) 一.安装squid # yum -y install squid 二.编辑配置文件(正向代理) # vim /etc/sq ...

  6. 國王遊戲(2012年NOIP全国联赛提高组)

    题目描述 Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n位大臣排成 ...

  7. kuangbin_ShortPath D (POJ 3268)

    本来在想 单源多点很好解决但是多源单点怎么解 然后我发现只要倒过来就可以了 把输入存下来然后 处理完dis1 重新init一次 倒着再输入一次 处理dis2 输出max(dis1[i] + dis2[ ...

  8. (转)word2vec前世今生

    word2vec 前世今生 2013年,Google开源了一款用于词向量计算的工具——word2vec,引起了工业界和学术界的关注.首先,word2vec可以在百万数量级的词典和上亿的数据集上进行高效 ...

  9. JSBinding + SharpKit / Coroutine支持

    首先得深入了解协程的原理.如果还没有完全理解,建议看这篇: http://wiki.unity3d.com/index.php/CoroutineScheduler 另外还要对 JavaScript ...

  10. 谷歌浏览器chrome与firefox的冲突(未解之谜)

    那年,公司开发了一套在线制作电子书的系统 e-textbook. 我负责小学电脑科教材在线题目的制作. 利用 ps制作剪裁好图片,导入系统,制作题目,并通知同事添加代码. 检测时,却发现有一道图片拖放 ...