题目:https://codeforces.com/problemset/problem/1323/C
题解:
有一个特殊的点位是“)(”类型的,虽然两个数量都是1,但是顺序不正确,加上这个就应该能过了
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
template <typename T>
void read(T &x) {
int s = 0, c = getchar();
x = 0;
while (isspace(c))
c = getchar();
if (c == 45)
s = 1, c = getchar();
while (isdigit(c))
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (s)
x = -x;
}
template <typename T>
void write(T x, char c = ' ') {
int b[40], l = 0;
if (x < 0)
putchar(45), x = -x;
while (x > 0)
b[l++] = x % 10, x /= 10;
if (!l)
putchar(48);
while (l)
putchar(b[--l] | 48);
putchar(c);
}
int main(void) {
// int kase;
// read(kase);
// for (int ii = 1; ii <= kase; ii++) {
int n;
read(n);
//getchar();
char s[1000010];
scanf("%s",&s);
int ans=0,sum=0;
for(int i=0;i<n;i++){
if(s[i]=='(')ans++;else ans--;
if(ans<0||ans==0&&s[i]=='(') sum++;
}
printf("%d\n", ans != 0 ? -1 : sum);
// }
return 0;
}
参考:https://xiaoxiaoh.blog.csdn.net/article/details/104749170