题意:
codeforces 917A Monster
思路:
First, let's denote s[l..r] as the substring slsl + 1... sr of string s. Also s.count(t) is the number of occurrences of t in s.
A string consisting of parentheses and question marks is pretty if and only if:
1、|s| is even.
2、0 ≤ s[1..i].count('(') + s[1..i].count('?') - s[1..i].count(')') for each 1 ≤ i ≤ |s|.
3、0 ≤ s[i..|s|].count(')') + s[i..|s|].count('?') - s[i..|s|].count('(') for each 1 ≤ i ≤ |s|.
反思:
脑洞确实不够大。
不过吃一堑长一智吧,以后遇到相似问题应该能有所启发。
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000 + 10;
char s[MAXN];
int vis[MAXN][MAXN];
int main()
{
scanf("%s", s);
int ans = 0;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
int a = 0, b = 0, c = 0;
for(int j = i; j < len; j++)
{
if(s[j] == '(') a++;
else if(s[j] == '?') b++;
else c++;
if(a + b - c >= 0)
{
if((j - i + 1) % 2 == 0) vis[i][j]++;
}
else break;
}
}
for(int i = len - 1; i >= 0; i--)
{
int a = 0, b = 0, c = 0;
for(int j = i; j >= 0; j--)
{
if(s[j] == ')') a++;
else if(s[j] == '?') b++;
else c++;
if(a + b - c >= 0)
{
if(vis[j][i] && (i - j + 1) % 2 == 0) vis[j][i]++, ans++;
}
else break;
}
}
printf("%d\n", ans);
return 0;
}