4 [] ([])[] ((] ([)]
0 0 3 2
1 #include<cstdio> 2 #include<cstring> 3 int main(){ 4 int n; 5 scanf("%d",&n); 6 while(n--){ 7 char arry[105] = {‘0‘},stack[105] = {‘0‘}; 8 scanf("%s",arry); 9 int len = strlen(arry),i,flag,temp,sum = 0; 10 int top = 0,k; 11 for(i = 0;i < len;i++){ 12 temp = flag = 0; 13 k = top; 14 if(arry[i] == ‘(‘ || arry[i] == ‘[‘) 15 stack[top++] = arry[i]; 16 else if(arry[i] == ‘)‘ && stack[top - 1] == ‘(‘ || arry[i] == ‘]‘ && stack[top - 1] == ‘[‘){ 17 top--; 18 }else{ 19 while(k!=0){ 20 if(arry[i] == ‘)‘ && stack[k - 1] == ‘(‘ || arry[i] == ‘]‘ && stack[k - 1] == ‘[‘){ 21 temp = 1; 22 top = k - 1; 23 break; 24 }else{ 25 flag += 1; 26 } 27 k--; 28 } 29 if(!temp) 30 sum += 1; 31 else 32 sum += flag; 33 } 34 } 35 sum += top; 36 printf("%d\n",sum); 37 } 38 return 0; 39 }
当然也可以用DP的思想,先挖好坑,待研究。。。
原文:http://www.cnblogs.com/zhengbin/p/4455667.html