UVA 2451 Brackets sequence

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=452

用dp[i][j] 记录一段序列,starts from index i, ends with index j,需要添加的char的个数。对于一段序列i~j,如果i, j 配对的话,那么dp[i][j]=dp[i+1][j-1].如果i, j不配对的话,那就需要对序列进行分割,dp[i][j]=dp[i][k]+dp[k+1][j],对k进行for loop找到最佳的k。但是当i, j配对时,并不代表dp[i+1][j-1]是最佳的结果,仍旧需要寻找最佳的k。代码如下:

  1 #include <iostream>
  2 #include <math.h>
  3 #include <stdio.h>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <string.h>
  7 #include <string>
  8 #include <sstream>
  9 #include <cstring>
 10 #include <queue>
 11 #include <vector>
 12 #include <functional>
 13 #include <cmath>
 14 #include <set>
 15 #define SCF(a) scanf("%d", &a)
 16 #define IN(a) cin>>a
 17 #define FOR(i, a, b) for(int i=a;i<b;i++)
 18 #define Infinity 9999999
 19 typedef long long Int;
 20 using namespace std;
 21
 22 int path[105][105];
 23 char brackets[105];
 24 void sprint(int s, int e)
 25 {
 26     if (s > e)
 27         return;
 28     if (s == e)
 29     {
 30         if (brackets[s] == ‘(‘ || brackets[s] == ‘)‘)
 31             printf("()");
 32         else
 33             printf("[]");
 34     }
 35     else
 36     {
 37         if (path[s][e] == -1)
 38         {
 39             printf("%c", brackets[s]);
 40             sprint(s + 1, e - 1);
 41             printf("%c", brackets[e]);
 42         }
 43         else
 44         {
 45             sprint(s, path[s][e]);
 46             sprint(path[s][e] + 1, e);
 47         }
 48     }
 49 }
 50
 51 int main()
 52 {
 53     int N;
 54     SCF(N);
 55     cin.get();
 56     int len = 0;
 57     int dp[105][105];
 58     FOR(casenum, 0, N)
 59     {
 60         cin.get();
 61         cin.getline(brackets, 105);
 62         len = 0;
 63         for (len = 0; brackets[len] != ‘\0‘; len++);
 64
 65         if (casenum)
 66             printf("\n");
 67         if (len == 0)
 68         {
 69             printf("\n");
 70             continue;
 71         }
 72         FOR(i, 0, len)
 73         {
 74             FOR(j, 0, len)
 75                 dp[i][j] = 0;
 76         }
 77         FOR(i, 0, len)
 78             dp[i][i] = 1;
 79         int end = 0;
 80
 81         FOR(clen, 1, len)
 82         {
 83             FOR(start, 0, len - clen)
 84             {
 85                 end = start + clen;
 86                 dp[start][end] = Infinity;
 87
 88                 if ((brackets[start] == ‘(‘ && brackets[end] == ‘)‘) || (brackets[start] == ‘[‘ && brackets[end] == ‘]‘))
 89                 {
 90                     dp[start][end] = dp[start + 1][end - 1];
 91                     path[start][end] = -1;
 92                 }
 93
 94                 for (int k = start; k < end; k++)
 95                 {
 96                     if (dp[start][end] > dp[start][k] + dp[k + 1][end])
 97                     {
 98                         dp[start][end] = dp[start][k] + dp[k + 1][end];
 99                         path[start][end] = k;
100                     }
101                 }
102
103
104             }
105         }
106         sprint(0, len - 1);
107         printf("\n");
108     }
109
110     return 0;
111 }
时间: 2024-10-19 08:40:58

UVA 2451 Brackets sequence的相关文章

uva 1626 Brackets Sequence ?(动态规划)

状态表示方法:d[ i ][ j ]表示的是一条序列的开始和结束: 状态定义:d[ i ][ j ]表示字串s[ i~j ] 需要添加的数量. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; char s[105]; int d[105][105]; bool match(char ch1,char ch2) { if((ch1=='['&&am

UVa 1626 Brackets sequence (动态规划)

题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配,枚举中间断点k,如果str[i]==str[k]则dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k+1][j])表示不需要加入新的括号,也可以插入新的括号则dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]+1). 如果区间最左边字符

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}; // 但是无

POJ1141 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, then AB is a regular

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

【POJ 1141】Brackets Sequence

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27996   Accepted: 7936   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: 27793   Accepted: 7885   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

LIS UVA 10534 Wavio Sequence

题目传送门 1 /* 2 LIS:应用,nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理. 3 因为是对称的,所以取较小值*2-1再取最大值 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 21:38:32 8 * File Name :UVA_10534.cpp 9 ***************

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, then AB is a regular s