15 matching parentheses (b)
Memory limit: 64MB Time limit: 1000ms special sentence: No
by number: 91 Submissions: 276 Difficulty: 6
Subject 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
([]) [] is matched
((] is not matched
([)] is mismatched
Such as:
[] is matched
([]) [] is matched
((] is not matched
([)] is mismatched
Enter a description:
The first line enter a positive integer N, the number of test data (N <= 10) sets each have only one row of test data, a string S, S containing only four characters mentioned above, no length S More than 100
Output Description:
For each set of test data is output a positive integer indicating the minimum number of brackets to be added. Output row for each test
Sample input:
4 [] ([])[] ((] ([)]
Sample output:
0 0 . 3 2
Reference LeetCode32 longest valid parentheses solution.
LeetCode32 longest valid parentheses using methods remove all perfect string matching substring After the string is assumed treatment stringBuffer. On the basis of this string will be much simpler to do.
For perfect no substring string s, using d [i] [j] represents the substring [i, j) require a minimum number of brackets added. Note that strings like need another determination, such as (])
state transition equation is:
1 import java.util.Scanner; 2 3 public class Main{ 4 static class Solution { 5 boolean match(char a, char b) { 6 if (a == '(' && b == ')' || a == '[' && b == ']') 7 return true; 8 return false; 9 } 10 11 public int solve(String s) { 12 if (s.length()<=1) return s.length(); 13 int[][] d = new int[s.length()+1][s.length()+1]; 14 15 for (int i = 0; i < s.length(); i++) { 16 d[i][i] = 0; 17 d[i][i + 1] = 1; 18 } 19 20 for (int step = 2; step <= s.length(); step++) { 21 for (int i = 0; i < s.length() && i+step<s.length()+1; i++) { 22 if (match(s.charAt(i), s.charAt(i + step - 1))) { 23 d[i][i+step]=d[i+1][i+step-1]; 24 }else { 25 int min = 65535; 26 for (int k = i + 1; k < i + step; k++) { 27 min = Math.min(d[i][k] + d[k][i + step], min); 28 } 29 d[i][i + step] = min; 30 } 31 } 32 } 33 return d[0][s.length()]; 34 } 35 } 36 37 [ public static the StringBuffer deSubPerfect (String S) { 38 is int [] D = new new int [s.length ()]; 39 int max = 0 ; 40 41 is for ( int I =. 1; I <s.length (); i ++ ) { 42 is // find the most perfect i matches the end position 43 is int len = 0 ; 44 is IF (. 1-i-D [. 1-i]> = 0 && (s.charAt (i) == ')' && '(' == s.charAt (i - 1 - d [i - 1]) || s.charAt (i) == ']' && '[' == s.charAt (i-1 -d [-I. 1]))) { // (()) 45 len=d[i-1]+2; 46 }else if (!(s.charAt(i)==')' && s.charAt(i-1)=='(') || !(s.charAt(i)==']' && '['==s.charAt(i-1-d[i-1]))){// ()) 47 len=0; 48 }else {// (() || () 49 len = 2; 50 } 51 52 if (i-len>=0 && len!=0){ 53 d[i]=d[i-len]+len; d [i] =55{ else }54len; 56 } 57 max = (max < d[i]) ? d[i] : max; 58 } 59 StringBuffer s1 = new StringBuffer(); 60 for (int i = s.length()-1; i >= 0; i--) { 61 i-=d[i]; 62 if (i>=0) 63 s1.append(s.charAt(i)); 64 } 65 StringBuffer reverse = s1.reverse(); 66 return reverse; 67 } 68 69 public static void main(String[] args) { 70 String s = new String(); 71 Scanner sc = new Scanner(System.in); 72 Solution solution = new Solution(); 73 int n = Integer.parseInt(sc.nextLine()); 74 for (int i = 0; i < n; i++) { 75 s=sc.nextLine(); 76 StringBuffer stringBuffer = deSubPerfect(s); 77 78 int solve = solution.solve(stringBuffer.toString()); 79 System.out.println(solve); 80 } 81 } 82 } 83 //()(]() 84 //(())(())