Brackets Sequence
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 24393 | Accepted: 6885 | 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 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.
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
The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.
Output
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
([(]
Sample Output
()[()]
Source
思路:dp+dfs 搜索
写了很长的代码,本以为要调试好久呢,结果没有调试,没有注意到有输入空行的时候,用的scanf输入的,给为gets后ac了
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define N 110
#define INF 0x7fffff
using namespace std;
char s1[N];
char ans[2*N],temp[N];
int dp[N][N];
struct num
{
char c,next;
}a[N*4],b[N*4];
int back[N],front[N],Top1,Top2,Top3;
int main()
{
//freopen("data.txt","r",stdin);
void dfs(int l,int r);
while(gets(s1))
{
memset(dp,0,sizeof(dp));
int l = strlen(s1);
for(int i=l-1;i>=0;i--)
{
s1[i+1] = s1[i];
}
for(int i=1;i<=l;i++)
{
for(int j=i;j>=1;j--)
{
if(s1[i]=='['||s1[i]=='(')
{
if(j==i)
{
dp[j][i] = 1;
}else
{
dp[j][i] = dp[j][i-1]+1;
}
continue;
}
if(j==i)
{
dp[j][i] = 1;
continue;
}
int Min = INF;
for(int u=i;u>=j;u--)
{
int s2=0,s3=0;
if((s1[u]=='['&&s1[i]==']')||(s1[u]=='('&&s1[i]==')'))
{
if(j<=u-1)
{
s2+= dp[j][u-1];
}
if(u+1<=i-1)
{
s2+=dp[u+1][i-1];
}
Min = min(Min,s2);
}
if(j<=u-1)
{
s3+=(dp[j][u-1]);
}
if(u<=i-1)
{
s3+=dp[u][i-1];
}
s3+=1;
Min = min(Min,s3);
}
dp[j][i] = Min;
}
}
memset(back,-1,sizeof(back));
memset(front,-1,sizeof(front));
Top1 = 0;
Top2 = 0;
dfs(1,l);
Top3 = 0;
for(int i=1;i<=l+1;i++)
{
int Top4 = 0;
if(i!=1)
{
Top4 = 0;
for(int j=back[i-1];j!=-1;j=a[j].next)
{
temp[Top4++] = a[j].c;
}
}
if(i!=l+1)
{
for(int j=front[i];j!=-1;j=b[j].next)
{
temp[Top4++] = b[j].c;
}
}
for(int j=Top4-1;j>=0;j--)
{
ans[Top3++] = temp[j];
}
if(i!=l+1)
{
ans[Top3++] = s1[i];
}
}
ans[Top3] = '\0';
printf("%s\n",ans);
}
return 0;
}
void addeage1(int x,char c)
{
a[Top1].c = c;
a[Top1].next = back[x];
back[x] = Top1++;
}
void addeage2(int x,char c)
{
b[Top2].c = c;
b[Top2].next = front[x];
front[x] = Top2++;
}
void dfs(int l,int r)
{
if(s1[r]=='('||s1[r]=='[')
{
if(s1[r]=='(')
{
addeage1(r,')');
}else
{
addeage1(r,']');
}
if(l<r)
{
dfs(l,r-1);
}
}else
{
if(l==r)
{
if(s1[r]==')')
{
addeage2(l,'(');
}else
{
addeage2(l,'[');
}
return ;
}
int ll,lr,rl,rr;
int k1=0,k2=0;
for(int i=l;i<=r;i++)
{
int sum = 0;
k1 = k2 = 0;
if(l<=i-1)
{
sum += dp[l][i-1];
k1 = 1;
ll = l;
lr = i-1;
}
if(i<=r-1)
{
sum+=dp[i][r-1];
k2 = 1;
rl = i;
rr = r-1;
}
sum+=1;
if(sum==dp[l][r])
{
if(s1[r]==')')
{
addeage2(i,'(');
}else
{
addeage2(i,'[');
}
break;
}
k1 = k2 = 0;
if((s1[i]=='('&&s1[r]==')')||(s1[i]=='['&&s1[r]==']'))
{
sum = 0;
if(l<=i-1)
{
sum+=dp[l][i-1];
k1 = 1;
ll = l;
rl = i-1;
}
if(i+1<=r-1)
{
sum+=dp[i+1][r-1];
k2 = 1;
rl = i+1;
rr = r-1;
}
if(sum==dp[l][r])
{
break;
}
}
}
if(k1)
{
dfs(ll,lr);
}
if(k2)
{
dfs(rl,rr);
}
}
}