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.

  1 //    1141
  2 //    http://blog.sina.com.cn/s/blog_8e6023de01014ptz.html
  3 //
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <memory.h>
  7
  8 #define MAX_LEN 101
  9
 10 bool isMatched(char *in, int i, int j)
 11 {
 12     return (in[i] == ‘(‘ && in[j] == ‘)‘) || (in[i] == ‘[‘ && in[j] == ‘]‘);
 13 }
 14 void printPath(int path[MAX_LEN][MAX_LEN], int i, int j, char in[MAX_LEN])
 15 {
 16     int sInx = path[i][j];
 17     if (sInx == -1)
 18     {
 19         if (i == j)
 20         {
 21             //printf("Complete @ %d\n", i);
 22             switch (in[i])
 23             {
 24             case ‘(‘:
 25             case ‘)‘: printf("()"); break;
 26             case ‘[‘:
 27             case ‘]‘: printf("[]"); break;
 28             }
 29             return;
 30         }
 31         else if (i + 1 == j)
 32         {
 33             //printf("Already matched: [%d, %d]\n", i, j);
 34             printf("%c%c", in[i], in[j]);
 35             return;
 36         }
 37         else if ((i+1) < j)
 38         {
 39             printf("%c", in[i]);
 40             printPath(path, i + 1, j - 1, in);
 41             printf("%c", in[j]);
 42         }
 43     }
 44     else
 45     {
 46         printPath(path, 0, path[i][j], in);
 47         //printf("Break @ %d\n", path[i][j], in);
 48         printPath(path, path[i][j] + 1, j, in);
 49     }
 50 }
 51
 52 void calc(char in[MAX_LEN])
 53 {
 54     unsigned slen = strlen(in);
 55     if (slen == 0)
 56     {
 57         printf("\n");
 58         return;
 59     }
 60     else if (slen > 0)
 61     {
 62         int dp[MAX_LEN][MAX_LEN];
 63         int path[MAX_LEN][MAX_LEN];
 64
 65         //    Init
 66         for (int i = 0; i < MAX_LEN; i ++)
 67         for (int j = 0; j < MAX_LEN; j++)
 68         {
 69             dp[i][j] = 0xFFFFFF;
 70             path[i][j] = -1;
 71         }
 72
 73         //    Def: dp[i][j] = min num of chars to add, from i to j
 74         //    Recurrence relation:
 75         //    1. dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]), where k in (i..j)
 76         //    2. dp[i][j] = dp[i+1][j-1], <IF in[i]:in[j] = [] or ()>
 77
 78         //    i..j is range, k is interval
 79         for (int k = 0; k < slen; k++)    // NOTE: this is a bottom-up DP. We have to go from 0 to slen as interval
 80         for (int i = 0, j = i + k; i < slen - k; i ++, j ++)
 81         {
 82             if (i == j)
 83             {
 84                 dp[i][j] = 1;
 85                 path[i][j] = -1;
 86                 continue;
 87             }
 88             bool bIsMatched = isMatched(in, i, j);
 89             if (bIsMatched)        // eq 2
 90             {
 91                 if (k == 1)        // length == 2
 92                 {
 93                     dp[i][j] = 0;
 94                     path[i][j] = -1;
 95                     continue;
 96                 }
 97                 else if (k > 1)    // length > 2
 98                 {
 99                     dp[i][j] = dp[i + 1][j - 1];
100                     path[i][j] = -1;    // we don‘t split matched pair
101                     //    A: we still go ahead with eq1
102                 }
103             }
104             //else    // eq1
105             {
106                 //    t is iterator of split index
107                 for (int t = i; t < j; t++)
108                 {
109                     int newVal = dp[i][t] + dp[t + 1][j];
110                     if (newVal <= dp[i][j]) // Label A: we prefer splitted solution
111                     {
112                         dp[i][j] = newVal;
113                         path[i][j] = t;
114                     }
115                 }
116             }
117         }
118         printPath(path, 0, slen - 1, in);
119     } //    if (slen > 0)
120 }
121
122 int main()
123 {
124     char in[MAX_LEN] = { 0 };
125     gets(in);
126     calc(in);
127     printf("\n");
128     return 0;
129 }

POJ #1141 - Brackets Sequence - TODO: POJ website issue

时间: 2024-11-15 14:23:37

POJ #1141 - Brackets Sequence - TODO: POJ website issue的相关文章

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

[ACM] POJ 1141 Brackets Sequence (区间动态规划)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25087   Accepted: 7069   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

POJ 1141 Brackets Sequence (区间dp 记录路径)

题目大意: 给出一种不合法的括号序列,要求构造出一种合法的序列,使得填充的括号最少. 思路分析: 如果只要求输出最少的匹配括号的数量,那么就是简单的区间dp dp[i][j]表示 i - j 之间已经合法了最少添加的括号数. 转移 就是 dp[i] [j] = min  (dp[i+1][j]+1 , dp[ i+ 1] [ k -1 ] + dp[k+1] [j] (i k 位置的括号匹配)) 其次我们要记录路径,你发现  如果 dp [i] [j] 是由 dp [i+1] [j] 转移过来的

POJ 1141 Brackets Sequence (线性dp 括号匹配 经典题)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26407   Accepted: 7443   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

poj 1141 Brackets Sequence (区间DP)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25893   Accepted: 7295   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

区间DP [POJ 1141] Brackets Sequence

Brackets Sequence Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, th

POJ 1141 Brackets Sequence (区间DP)

Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular

poj 1141 Brackets Sequence

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32662   Accepted: 9441   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

poj 1141 Brackets Sequence dp

 Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regu