CF2144F.Bracket Groups

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic sequence. For example:

  • bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
  • bracket sequences ")(", "(" and ")" are not.

You are given nn bracket sequences s1,s2,…,sns_1, s_2, \dots, s_n (not necessarily regular) and an even integer kk. Your task is to split all of them into groups in such a way that:

  • every bracket sequence belongs to exactly one group;
  • for each group, there exists a regular bracket sequence of length exactly kk such that every bracket sequence assigned to this group is not a substring of it.

What is the smallest number of groups that this can be achieved for? If it's impossible to do for any number of groups, report so. Otherwise, print the groups and any valid regular bracket sequence for each group. If there are multiple answers with the smallest number of groups, print any of them.

正则括号序列是指能够被转化为合法算术表达式的括号序列。例如:

  • 括号序列 "()()" 和 "(())" 是正则的(对应的表达式分别为 "(1)+(1)" 和 "((1+1)+1)");
  • 括号序列 ")(", "(" 和 ")" 不是正则的。

给定 nn 个括号序列 s1,s2,…,sns_1, s_2, \dots, s_n(未必是正则的)以及一个偶数 kk。你的任务是将所有这些括号序列划分为若干组,使得:

  • 每个括号序列恰好属于一个组;
  • 对每个组,存在一个长度恰好为 kk 的正则括号序列,使得该组中所有括号序列均不是它的子串(即不作为连续子序列出现)。

求实现上述条件所需的最少组数。若无论使用多少组都无法满足条件,则报告不可行。否则,请输出各组的划分方式,以及为每组指定的一个合法的、长度恰为 kk 的正则括号序列。若存在多个具有最少组数的解,输出任意一个即可。

输入格式

The first line contains two integers nn and kk (1≤n≤501 \le n \le 50; 2≤k≤502 \le k \le 50; kk is even).

The ii-th of the next nn lines contains a bracket sequence sis_i (2≤∣si∣≤k2 \le |s_i| \le k). It consists only of characters '(' and ')'.

第一行包含两个整数 nn 和 kk(1≤n≤501 \le n \le 50;2≤k≤502 \le k \le 50;kk 为偶数)。

接下来的 nn 行中,第 ii 行包含一个括号序列 sis_i(2≤∣si∣≤k2 \le |s_i| \le k),该序列仅由字符 '(' 和 ')' 组成。

输出格式

If it's impossible to split all bracket sequences into groups according to the rules, then print −1-1.

Otherwise, print the smallest possible number of groups mm in the first line. Then, mm blocks of data should follow:

  • The first line of each block should contain a regular bracket sequence of length kk.
  • The second line should contain a single integer gg — the number of bracket sequences in the current group.
  • The third line should contain gg integers from 11 to nn — the indices of the bracket sequences that belong to the current group.

None of these bracket sequences should be a substring of the chosen regular bracket sequence. Each index from 11 to nn should belong to exactly one group.

If there are multiple answers with the smallest mm, print any of them.

如果无法按照规则将所有括号序列划分为若干组,则输出 −1-1。

否则,在第一行输出最小可能的组数 mm。随后应输出 mm 个数据块:

  • 每个数据块的第一行应包含一个长度为 kk 的合法括号序列;
  • 第二行应包含一个整数 gg —— 当前组中括号序列的个数;
  • 第三行应包含 gg 个介于 11 到 nn 之间的整数 —— 属于当前组的括号序列的索引。

这些括号序列中,任意一个都不能是所选合法括号序列的子串。每个从 11 到 nn 的索引必须且仅属于一个组。

若存在多个具有最小 mm 的答案,输出其中任意一个即可。

输入输出样例

  • 输入#1

    3 6
    )))
    (((
    (())

    输出#1

    1
    (()())
    3
    1 2 3
  • 输入#2

    4 6
    (()
    ()(
    ())
    ((((((

    输出#2

    2
    (())()
    1
    2
    ()()()
    3
    1 4 3
  • 输入#3

    2 6
    ()
    )(

    输出#3

    -1

输入解题思路,AI测评打分。不知道怎么写?

首页