UVA - 1626 Brackets sequence ( dp )
2017-08-09 17:09
253 查看
原题:
Let us defie 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 fid the shortest possible
regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string
a 1a
2. . . a
n
is called a subsequence of the stringb
1b
2. . . b
m, if there exist such indices1
≤i
1 < i2
<
. . . < in≤
m, thataj=
bij
for all1
≤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 fie 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 fie 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
()[()]
题意:
给一行由'('')''['']'组成的序列,求最少添加几个字符可以使整个序列全部括号匹配。
思路:
取dp[i][j]表示字串s[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 }。题目还有两个坑点,一个是可能输出空串,并且输入空串时还应对应输出一个空串;另一个是每个相邻样例间要有一个输出一个空行,并且最后一个样例后不输出空行。导致WA了好几遍……
Let us defie 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 fid the shortest possible
regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string
a 1a
2. . . a
n
is called a subsequence of the stringb
1b
2. . . b
m, if there exist such indices1
≤i
1 < i2
<
. . . < in≤
m, thataj=
bij
for all1
≤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 fie 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 fie 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
()[()]
题意:
给一行由'('')''['']'组成的序列,求最少添加几个字符可以使整个序列全部括号匹配。
思路:
取dp[i][j]表示字串s[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 }。题目还有两个坑点,一个是可能输出空串,并且输入空串时还应对应输出一个空串;另一个是每个相邻样例间要有一个输出一个空行,并且最后一个样例后不输出空行。导致WA了好几遍……
#include <iostream> #include <iomanip> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <deque> #include <string> #include <cmath> #include <vector> #include <utility> #include <set> #include <map> #include <climits> //#pragma comment(linker, "/STACK:1024000000,1024000000") #define INF 2147483647 using namespace std; typedef long long ll; int t; string s; int n,dp[105][105]; bool match(string ss,int a,int b) { if(ss[a]=='('&&ss[b]==')') return true; else if(ss[a]=='['&&ss[b]==']') return true; else return false ; } void print(int i,int j) { if(i>j) return ; if(i==j) { if(s[i]=='('||s[i]==')') printf("()"); else printf("[]"); return ; } int ans=dp[i][j]; if(match(s,i,j)&&ans==dp[i+1][j-1]) { cout<<s[i]; print(i+1,j-1); cout<<s[j]; return ; } for(int k=i;k<j;k++) if(ans==dp[i][k]+dp[k+1][j]) { print(i,k); print(k+1,j); return ; } } int main() { scanf("%d",&t); getchar(); while(t--) { getchar(); s.clear(); //cin>>s; getline(cin,s); n=s.size(); memset(dp,0,sizeof(dp)); for(int i=0; i<n; i++) 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,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"); } return 0; }
相关文章推荐
- UVA 1626 Brackets sequence 区间DP
- 例题9-10 UVA 1626 Brackets sequence (dp递推 || 记忆化搜索)
- UVA - 1626 Brackets sequence DP
- NYOJ15 - UVA1626 括号匹配问题(区间dp)
- UVA 1626 Brackets sequence——dp
- dp uva1626 括号序列
- UVa 1626:Brackets Sequence(DP)
- UVA 1626 括号序列(区间dp)
- uva 1626 - Brackets sequence【DP】
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
- UVA 1626 - Brackets sequence 区间DP
- [UVA1626]Brackets sequence(dp)
- POJ 1141 / UVa 1626 Brackets Sequence (区间DP&打印路径)
- Brackets sequence UVA - 1626 区间dp
- NYOJ15 - UVA1626 括号匹配问题(dp)
- UVA1626 DP经典
- Brackets sequence UVA - 1626(区间dp)
- Brackets sequence UVA - 1626 (典型的区间dp+递归打印路径)
- UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)
- 区间DP(括号序列,uva1626)