A valid bracket matching sequence is defined as:
1. Empty string "" is a valid bracket sequence
2. If "X" and "Y" are legitimate sequences, then "XY" is also a valid bracket sequence.
3. If "X" is a legitimate sequence, then "(x)" is also a valid bracket sequence
4. Each valid bracket sequence can be generated by the rule above
For example "", "()", "() () ()", "(() ())", "((()))" are lawful.
A sequence that removes 0 or more characters from a string s is called a subsequence of S.
For example, the subsequence of "ABCDE" has "Abe", "" "," ABCDE "and so on.
Defines the length of the LCS (S,t) as the longest common subsequence of the string s and the string T, that is, the longest sequence w is both the subsequence of S and the length of the subsequence of T.
Small easy to give a valid bracket matching sequence s, small easy hope you can find out with the following characteristics of the parentheses sequence T:
1, T and s different, but the same length
2, T is also a valid bracket matching sequence
3. The LCS (S, t) is the largest of the t that satisfies the above two conditions
Because such a t may exist multiple, small easy to need you to calculate the number of t that satisfies the condition.
As shown in the sample: S = "(()) ()", the same valid bracket matching sequence as the string s length is:
"() (())", "(())", "() () () ()", "(() ())", where the LCS ("()) ()", "() ()") is 4, the other three are 5, so the output is 3.
Enter a description:
The input includes the string s (4≤|s|≤50,|s| represents the string length), which guarantees that s is a valid bracket matching sequence.
Output Description:
Outputs a positive integer that satisfies the number of t of the condition.
Enter Example 1:
(())()
Output Example 1:
3
Analysis: Regardless of what type of string s is entered, there must be at least one t that makes the LCS (S,t) =len-1. So the problem can be converted to {count (T), LCS (s,t) =len-1}. So you can traverse the string s and insert the current string anywhere in the remaining string, if the valid parentheses are recorded. Finally go heavy.
The code is as follows:
Import java.util.*;
public class main{public static void Main (String[]args) {Scanner in=new Scanner (system.in);
String S=in.nextline ();
System.out.println (Work (s));
public static int work (String s) {set<string> set=new hashset<string> ();
int Len=s.length ();
for (int i=0;i<len;i++) {StringBuilder st=new StringBuilder ();
St.append (s.substring (0,i));
St.append (s.substring (i+1));
for (int j=0;j<len-1;j++) {StringBuilder sw=new StringBuilder ();
Sw.append (st.substring (0, J));
Sw.append (S.charat (i));
Sw.append (St.substring (j));
if (Legal (sw.tostring ())) Set.add (sw.tostring ());
} return Set.size ()-1;
public static Boolean legal (String s) {int count=0; for (int i=0;i<s.length (); i++) {if (S.charat(i) = = ' (') count++;
else count--;
if (count<0) return false;
return count==0; }
}