找到牛
只有在后腿前面的前腿才能匹配上
C++
#include<string>
using namespace std;
int main()
{
string s;
cin >> s;
int n = 0, ans = 0;
for(int i = 0; i < s.size() - 1; i ++)
{
if(s[i]=='(' && s[i] == s[i + 1])n ++;
if(s[i] == ')' && s[i] == s[i + 1])ans += n;
}
cout << ans <<endl;
return 0;
}
JAVA
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
String s = cin.nextLine();
int a = 0, ans = 0;
for(int i = 0; i < s.length() - 1; i ++)
{
if(s.charAt(i) == '(' && s.charAt(i) == s.charAt(i + 1))a ++;
if(s.charAt(i) == ')' && s.charAt(i) == s.charAt(i + 1))ans += a;
}
System.out.println(ans);
}
}