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. linux定时

    linux怎样启动定时任务 crontab -e进入以后的,定时任务写入 */1 * * * * /usr/bin/python /root/lianxi/time_1.py ,每一分钟定时执行tim ...

  2. 用Xshell访问 虚拟机里的kali

    1.vim /etc/ssh/sshd_config 2.PermitRootLogin without-password复制 3.把一个PermitRootLogin without-passwor ...

  3. OVS ARP Responder – Theory and Practice

    Prefix In the GRE tunnels post I’ve explained how overlay networks are used for connectivity and ten ...

  4. CGI实现页面的动态生成

    传统的Web应用开发局限于有限的静态页面(HTML静态页面),不利于系统的扩展,不能提供及时信息,而且修改维护麻烦,所以建立一个动态Web应用程序尤为重要.一方面根据访问者的不同请求返回不同的访问信息 ...

  5. POJ 1502 MPI Maelstrom

    MPI Maelstrom Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total ...

  6. 横向滚动条展示 css

    <div class="shuaixuan" style="overflow:hidden;">    <div style="ov ...

  7. UVa 1584 Circular Sequence --- 水题

    UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...

  8. Questions that are independent of programming language. These questions are typically more abstract than other categories.

    Questions that are independent of programming language.  These questions are typically more abstract ...

  9. /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:No such file or directory的解决办法

    在编译32位HDecode时出现如题所示的错误,原因时没有安装32位glibc库导致的: ubuntu: sudo apt-get install libc6-dev-i386 CentOS:yum ...

  10. InvokeRequired和Invoke

    C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它.此时它将会在内部调用ne ...