CF917A 思维题

题意:
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.
20 ≤ s[1..i].count('(') + s[1..i].count('?') - s[1..i].count(')') for each 1 ≤ i ≤ |s|.
30 ≤ 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值