poj1141

Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19727   Accepted: 5465   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.

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

 

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 

 5 using namespace std;

 6 

 7 char str[110];

 8 int dp[110][110];

 9 int path[110][110];

10 

11 void output(int i, int j)

12 {

13     if(i>j)

14     {

15         return;

16     }

17     if(i==j)

18     {

19         if(str[i]=='('||str[i]==')')

20         {

21             printf("()");

22         }

23         else

24         {

25             printf("[]");

26         }

27     }

28     else if(path[i][j]==-1)

29     {

30         printf("%c",str[i]);

31         output(i+1,j-1);

32         printf("%c",str[j]);

33     }

34     else

35     {

36         output(i,path[i][j]);

37         output(path[i][j]+1,j);

38     }

39 }

40 

41 int main()

42 {

43     int len;

44     int i,j,k,r;

45 

46     while(gets(str))

47     {

48         len=strlen(str);

49         if(len==0)

50         {

51             printf("\n");

52             continue;

53         }

54 

55         //dp

56         memset(dp,0,sizeof(dp));

57         for(i=0; i<len; i++)

58         {

59             dp[i][i]=1;

60         }

61         for(r=1; r<len; r++)  //r表示i,j之间的距离

62         {

63             for(i=0;i<len-r;i++)

64             {

65                 j=i+r;

66                 dp[i][j]=65535;

67                 if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[j]==')'))

68                 {

69                     if(dp[i][j]>dp[i+1][j-1])

70                     {

71                         dp[i][j]=dp[i+1][j-1];

72                         path[i][j]=-1;

73                     }

74                 }

75                 for(k=i;k<j;k++)

76                 {

77                     if(dp[i][j]>(dp[i][k]+dp[k+1][j]))

78                     {

79                         dp[i][j]=dp[i][k]+dp[k+1][j];

80                         path[i][j]=k;

81                     }

82                 }

83             }

84         }

85         output(0,len-1);

86         printf("\n");

87     }

88 

89     return 0;

90 }

你可能感兴趣的:(poj)