457. The two matching brackets

[Title Description:

Give you a string, which contains only "(", ")" "[", "]" four kinds of symbols, how many would you need to add at least these brackets parentheses match up.

Such as:

[] Is matched

([]) [] Are matched

((] Do not match

([)] Is mismatched

[Input Description:

The first line enter a positive integer N, the number of test data sets (N <= 10)

Each test are only one line, is a string S, S contains only four characters mentioned above, the length S is not more than 200

[Output] Description:

For each set of test data is output a positive integer indicating the minimum number of brackets to be added. Each test output per line.

[Sample input]:

4 [] ([])[] ((] ([)]

[] Sample output:

0 0 3 2

[Time limit, and the range of data Description:

Time: 1s space: 64M

For 30% of the data: the length S no more than 10;

For 50% of the data: the length S is not more than 20;

To 100% of the data: the length S is not more than 200;

Code

#include<algorithm>
#include<iostream> #include<cstring> #include<queue> #include<cmath> #include<cstdio> using namespace std; int a[201][201]; char s[201]; int n; int main(){ scanf("%d\n",&n); while(n--){ scanf("%s",s+1); int len=strlen(s+1); memset(a,0,sizeof(a)); for(int i=len-1;i>=1;i--){ for(int j=i+1;j<=len;j++){ a[i][j]=a[i+1][j]; for(int k=i+1;k<=j;k++){ if(s[i]=='('&&s[k]==')' || s[i]=='[' && s[k]==']'){ a[i][j]=max(a[i][j],a[i+1][k-1]+a[k+1][j]+2); } } } } printf("%d\n",len-a[1][len]); } return 0; } 

Since the array did not open twice, so the TLE.

......(Silence is gold)

Silence is gold.

 

 

Guess you like

Origin www.cnblogs.com/xiongchongwen/p/11137628.html