当前位置: 代码迷 >> 综合 >> UVA1626 - Brackets sequence括号序列 DP(输入输出好坑)
  详细解决方案

UVA1626 - Brackets sequence括号序列 DP(输入输出好坑)

热度:87   发布时间:2023-12-24 06:50:46.0

题目

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 sequence.
For example, all of the following sequences of characters are regular brackets sequences:

And all of the following character sequences are not:

Some sequence of characters ‘(’, ‘)’, ‘[’, and ‘]’ is given. You are to find the shortest possible
regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string
a1a2 . . . an is called a subsequence of the string b1b2 . . . bm, if there exist such indices 1 ≤ i1 < i2 <
. . . < in ≤ m, that aj = bij
for all 1 ≤ j ≤ n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.//好坑,之前读的中文题意,这部分没有!!!

The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a
single line without any other characters among them.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases
will be separated by a blank line.
Write to the output file a single line that contains some regular brackets sequence that has the
minimal possible length and contains the given sequence as a subsequence.
Sample Input
1
([(]
Sample Output
()[()]

题意

定义如下正规括号序列(字符串):

  1. 空序列是正规括号序列。
  2. 如果S是正规括号序列,那么(S)和[S]也是正规括号序列。
  3. 如果A和B都是正规括号序列,那么AB也是正规括号序列。

例如,下面的字符串都是正规括号序列:(),[],(()),([]),()[],()[()],而如下字符串 则不是正规括号序列:(,[,],)(,([()。

 输入一个长度不超过100的,由“(”、“)”、“[”、“]”构成的序列,添加尽量少的括号,得 到一个规则序列。有多解输一个序列。

注意事项  

坑点来了

  1. 输入时要多输一个字符(只能用getchar();因为那个字符好像并不是'\n',不能用scanf("\n");)
  2. 输入字符组s时要输两遍因为很可能有空串
  3. 因为空串所以不能用scanf("%s",s)输入,只能用getchar,fgets,getline//算法书上这道题的最后有一排小字就写的这个,可惜我眼瞎(作者心黑),我之前用gets结果CE编译错误……,可能是它不得UVA的宠爱╭(╯^╰)╮,之后我别无他法,只能痛下杀手(痛快下手),用了fgets,希望它在天之灵能明白我的苦衷(都是UVA的错)o( ̄ヘ ̄o#)哼哼~。
  4. 输出的时候我放松了警惕,以为套路适可而止,可是UVA一点也不知道分寸。它要输换行(这里还没毛病)可是它要两个,两个,两个!!!并且,最后一组数据只输出一个换行。

(你可能觉得这些都是细节,可是,只要你一个没对,它就WA,WA,WA!!!)

思路

(终于到这了,真心不容易)

状态定义:

dp[i][j]:表示从s[i~j]至少需要添加几个括号

状态转移方程式:

if(match(s[i],s[j]))
      dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
for(int k=i;k<j;k++)//以k做断点(必须枚举判最优,不管是否匹配)
      dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define INF 0x3f
int t,n;
char s[102];
int dp[102][102];
bool match(char a,char b)
{return (a=='('&&b==')')||(a=='['&&b==']');
}
void print(int i,int j)
{if(i>j)return;if(i==j){if(s[i]=='('||s[j]==')')printf("()");else printf("[]");return;}int a=dp[i][j];if(match(s[i],s[j])&&a==dp[i+1][j-1]){printf("%c",s[i]);print(i+1,j-1);printf("%c",s[j]);return;}for(int k=i;k<j;k++)if(a==dp[i][k]+dp[k+1][j]){print(i,k);print(k+1,j);return;}
}
int main()
{scanf("%d",&t);while(t--){fgets(s,102,stdin);fgets(s,102,stdin);n=strlen(s)-1;memset(dp,0,sizeof(dp));for(int i=0;i<n;i++)dp[i+1][i]=0,dp[i][i]=1;for(int i=n-2;i>=0;i--)for(int j=i+1;j<n;j++){dp[i][j]=n;if(match(s[i],s[j]))dp[i][j]=min(dp[i][j],dp[i+1][j-1]);for(int k=i;k<j;k++)dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);}print(0,n-1);printf("\n");if(t)printf("\n");}
}

 

  相关解决方案