Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 32855 | Accepted: 9508 | Special Judge |
Description
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 a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
Input
Output
Sample Input
([(]
Sample Output
()[()]
题意:
给你一个括号字符串,你的任务是打印处配对好的括号字符串。
思路:区间dp加路径输出。路径输出比较难想。
dp[i][j] 表示从i到j 所需要配对的最小字符个数。(可以参考POJ 2955 )
http://blog.csdn.net/yjt9299/article/details/78150774
但是两个dp数组表示的意思正好相反。
这里对于这个题,还需要一个pos数组pos[i][j] 记录断开的位置,(从i到j)
dp方程: dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
代码:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define N 305 #define inf 0x3f3f3f using namespace std; int dp[N][N]; int pos[N][N]; char s[N]; void solve() { int i,j,k,d; memset(dp,0,sizeof(dp)); memset(pos,-1,sizeof(pos)); int len=strlen(s); for(i = 1; i < len; i++) dp[i][i-1] = 0; for(i=1;i<len;i++) dp[i][i]=1; for(d=1;d<len;d++) { for(i=0;i<len-d;i++) { j=d+i; dp[i][j]=inf; if((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']')) dp[i][j]=min(dp[i][j],dp[i+1][j-1]);//i ,j 字符匹配 for(k=i;k<j;k++) { if(dp[i][k]+dp[k+1][j]<dp[i][j]) { dp[i][j]=dp[i][k]+dp[k+1][j];//找到使得从i到j中需要配对的括号的个数为最小的k,也就是在位置k处进行括号配对。 pos[i][j]=k; } } } } return ; } void print(int l,int r) { if(l>r) return ; if(l==r){ if(s[l]=='('||s[l]==')') printf("()"); else if(s[l]=='['||s[l]==']') printf("[]"); return ; } else{ if(pos[l][r]!=-1) { print(l,pos[l][r]); print(pos[l][r]+1,r); } else{ if(s[l]=='('){ printf("("); print(++l,--r); printf(")"); } else if(s[l]=='['){ printf("["); print(++l,--r); printf("]"); } } } return ; } int main() { scanf("%s",s); int len=strlen(s); solve(); for(int i=0;i<len;i++) { for(int j=0;j<len;j++) { printf("%d ",pos[i][j]); } printf("\n"); } print(0,len-1); printf("\n"); return 0; }
在最后粘一下大神的思路:
解题思路:
根据“黑书”的思路,定义:
d[i][j]为输入序列从下标i到下标j最少需要加多少括号才能成为合法序列。0<=i<=j<len (len为输入序列的长度)。
c[i][j]为输入序列从下标i到下标j的断开位置,如果没有断开则为-1。
当i==j时,d[i][j]为1
当s[i]=='(' && s[j]==')' 或者 s[i]=='[' && s[j]==']'时,d[i][j]=d[i+1][j-1]
否则d[i][j]=min{d[i][k]+d[k+1][j]} i<=k<j , c[i][j]记录断开的位置k
采用递推方式计算d[i][j]
输出结果时采用递归方式输出print(0, len-1)
输出函数定义为print(int i, int j),表示输出从下标i到下标j的合法序列
当i>j时,直接返回,不需要输出
当i==j时,d[i][j]为1,至少要加一个括号,如果s[i]为'(' 或者')',输出"()",否则输出"[]"
当i>j时,如果c[i][j]>=0,说明从i到j断开了,则递归调用print(i, c[i][j]);和print(c[i][j]+1, j);
如果c[i][j]<0,说明没有断开,如果s[i]=='(' 则输出'('、 print(i+1, j-1); 和")"
否则输出"[" print(i+1, j-1);和"]"
黑书上的半括号的那种情况是多余的,包含在d[i][j]=min{d[i][k]+d[k+1][j]}这种情况里。
这种dp比较训练思维,不是常规的那种一眼就看出来的最优子结构。