P7938 [A] Beautiful Array--题解报告

题目

题目描述

In this problem, we define a sequence of ( and ) as a "bracket sequence".

The definition of Regular Bracket Sequence is as follows:

  1. () is a Regular Bracket Sequence.
  2. If A is a Regular Bracket Sequence, then (A) is also a Regular Bracket Sequence.
  3. If A and B are Regular Bracket Sequences, then AB is also a Regular Bracket Sequence.

For example: ()(()), and ()() are all Regular Bracket Sequences, but )(()( are not.

In particular, an empty sequence is not a Regular Bracket Sequence sequence in this problem.

Now cute Ran gives you a bracket sequence ss of length nn. She wants you to construct 2\cdot m2⋅m strictly increasing arrays. Let us denote them as p_1,p_2,\cdots,p_{2 m}p1​,p2​,⋯,p2m​ (you can leave any of them empty). You need to ensure that all integers between 1\sim n1∼n appear exactly once in these arrays.

An array p_i=\{r_1,r_2,\cdots,r_k\}pi​={r1​,r2​,⋯,rk​} is Beautiful if \{s_{r_1},s_{r_2},\cdots,s_{r_k}\}{sr1​​,sr2​​,⋯,srk​​} is a Regular Bracket Sequence.

Ran wonders whether it is possible to construct these arrays so that at least mm of the 2\cdot m2⋅m arrays are "beautiful arrays".

输入格式

Each test contains multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains two integers nn and mm, and the second line contains a bracket sequence ss.

输出格式

For each test case, print one line.

If it is possible to construct these arrays, print 11. Otherwise print 00.

题意翻译

定义一个字符串为括号串当且仅当其仅由 ( 和 ) 组成。

试将一个长度为 nn 的括号串分为 2m2m 个子序列,子序列可以为空,且每个字符都必须分到恰好一个子序列中,使得至少 mm 个子序列为匹配的括号串。空序列不算匹配的括号序列。无解请输出 00,否则输出 11。本题多组数据,其中数据组数为 TT。

定义 AA 为 BB 的子序列当且仅当 AA 能由 BB 在顺序不改变的前提下删除若干元素后得到。

*样例 11 解释:你可以将第一个和第二个字符分入第一个子序列,让第二个子序列为空子序列。此时第一个子序列为 (),第二个为空,总计有一个匹配的括号序列,满足要求。

输入输出样例

输入 #1复制

2
2 1
()
2 99
()

输出 #1复制

1
0

说明/提示

Explanation

For the first test case, we can construct p_1=\{1,2\}p1​={1,2} and p_2=\{\}p2​={}. So p_1p1​ is a "beautiful array".

For the second test case, it is obvious that we cannot use two numbers to construct 9999 beautiful arrays.

Constraints

1\le T,n,m\le 2001≤T,n,m≤200.

解题思想

这题大概是个语文题吧,我之前一直没看懂,题目的意思,到底是要干什么。

然后我好像大概理解了,我把它理解成括号匹配

输入t,代表接下来的例子个数

输入n,代表字符串长度

输入m,代表最少有几个括号

输入s,字符串

理解了题目意思,就可以直接利用栈,找出一共有多少个括号,再判断就好了。

代码展示

#include<stdio.h>
long long int n,m,t;
long long int top=0,sum=0;
char s[1010],q[1010];
int main()
{
    scanf("%lld",&t);
    while(t--)
    {
        top=0,sum=0;
        scanf("%lld %lld",&n,&m);
        scanf("%s",s);
        for(int i=0;s[i]!='\0'; i++)
        {
            if(s[i]=='(')//左括号入栈
                q[++top]=s[i];
            else if(top>=1)//匹配最近的右括号
            {
                top--;
                sum++;
            }
        }
        if(sum>=m)//满足要求
            printf("1\n");
        else printf("0\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值