CF172E.BHTML+BCSS

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This problem is about imaginary languages BHTML and BCSS, which slightly resemble HTML and CSS. Read the problem statement carefully as the resemblance is rather slight and the problem uses very simplified analogs.

You are given a BHTML document that resembles HTML but is much simpler. It is recorded as a sequence of opening and closing tags. A tag that looks like "" is called an opening tag and a tag that looks like "" is called a closing tag. Besides, there are self-closing tags that are written as "" and in this problem they are fully equivalent to "". All tagnames in this problem are strings consisting of lowercase Latin letters with length from 1 to 10 characters. Tagnames of different tags may coincide.

The document tags form a correct bracket sequence, that is, we can obtain an empty sequence from the given one using the following operations:

  • remove any self-closing tag "",
  • remove a pair of an opening and a closing tag that go consecutively (in this order) and have the same names. In other words, remove substring "".

For example, you may be given such document: "

" but you may not be given documents "", "", "" or "".

Obviously, for any opening tag there is the only matching closing one — each such pair is called an element. A self-closing tag also is an element. Let's consider that one element is nested inside another one, if tags of the first element are between tags of the second one. An element is not nested to itself. For instance, in the example above element "b" is nested in "header" and in "p", but it isn't nested in "a" and "footer", also it isn't nested to itself ("b"). Element "header" has three elements nested in it, and "footer" has zero.

We need the BCSS rules to apply styles when displaying elements of the BHTML documents. Each rule is recorded as a subsequence of words "_x_1 _x_2 ... x__n". This rule has effect over all such elements t, which satisfy both conditions from the list:

  • there is a sequence of nested elements with tagnames "_x_1", "_x_2", ..., "x__n" (that is, the second element is nested in the first one, the third element is nested in the second one and so on),
  • this sequence ends with element t (i.e. tagname of element t equals "x__n").

For example, element "b" meets the conditions of the rule "a b" if for element "b" exists element "a" in which it is nested. Element "c" meets the conditions of the rule "a b b c", if three elements exist: "a", "b", "b", and in the chain "a"-"b"-"b"-"c" each following element is nested in the previous one.

Given a BHTML document and a set of BCSS rules, write a program that determines the number of elements that meet the conditions of each rule.

本题涉及两种虚构语言:BHTML 和 BCSS,它们与 HTML 和 CSS 略有相似。请仔细阅读题目描述,因为这种相似性非常微弱,且本题使用的是高度简化的类比模型。

你将获得一个 BHTML 文档,该文档形似 HTML,但要简单得多。它被表示为一系列开始标签和结束标签的序列。形如 <tagname> 的标签称为开始标签,形如 </tagname> 的标签称为结束标签。此外,还存在自闭合标签,写作 <tagname/>;在本题中,其语义完全等价于 <tagname></tagname>。本题中所有标签名均由长度为 11 至 1010 的小写拉丁字母字符串组成。不同标签的标签名可以相同。

文档中的标签构成一个合法的括号序列,即我们可对给定序列反复执行如下操作,最终得到空序列:

  • 删除任意一个自闭合标签 <tagname/>;
  • 删除一对相邻(按此顺序出现)且标签名相同的开始标签与结束标签,即删除子串 <tagname></tagname>。

例如,你可能收到如下文档:<header><p><a/><b></b></p></header><footer></footer>;但你不会收到如下文档:<a>、<a></b>、</a><a> 或 <a><b></a></b>。

显然,对每个开始标签,有且仅有一个与其匹配的结束标签——每一对这样的标签称为一个元素。一个自闭合标签本身也构成一个元素。我们称一个元素嵌套在另一个元素内部,当且仅当前者的标签位于后者的开始标签与结束标签之间。一个元素不视为嵌套于自身之内。例如,在上述例子中,元素 b 嵌套在 header 和 p 内部,但不嵌套在 a 和 footer 内部,也不嵌套于自身(即 b)内部。元素 header 包含三个嵌套在其内部的元素,而 footer 则不包含任何嵌套元素。

我们需要借助 BCSS 规则,为 BHTML 文档中各元素应用样式。每条规则记作一个单词序列 _x_1 _x_2 ... _x__n_。该规则对所有满足以下两个条件的元素 t 生效:

  • 存在一个嵌套元素序列,其标签名依次为 _x_1, _x_2, ..., _x__n_(即第二个元素嵌套在第一个元素内,第三个元素嵌套在第二个元素内,依此类推);
  • 该序列以元素 t 结束(即元素 t 的标签名等于 _x__n_)。

例如,若存在一个嵌套在元素 a 内部的元素 b,则元素 b 满足规则 a b 的条件。又如,若存在三个元素 a、b、b,使得嵌套链为 a → b → b → c(即每个后续元素均嵌套于前一个元素内部),则元素 c 满足规则 a b b c 的条件。

给定一个 BHTML 文档及一组 BCSS 规则,请编写程序,统计满足每条规则条件的元素个数。

输入格式

The first line of the input contains a BHTML-document. The document has length from 4 to 106 characters. The document has a correct structure, doesn't contain spaces or any other unnecessary characters. Tagnames consist of lowercase Latin letters, their lengths are from 1 to 10 characters.

The second line contains an integer m (1 ≤ m ≤ 200) — the number of queries. Then m lines contain the queries, one per line. Each query is a sequence _x_1, _x_2, ..., x__n, where x__i is the i-th element of the query, and n (1 ≤ n ≤ 200) is the number of elements in the query. The elements are separated by single spaces. Each query doesn't begin with and doesn't end with a space. Each query element is a sequence of lowercase Latin letters with length from 1 to 10.

输入的第一行包含一个 BHTML 文档。该文档长度为 44 到 10610^6 个字符。文档结构正确,不包含空格或任何其他不必要的字符。标签名由小写拉丁字母组成,长度为 11 到 1010 个字符。

第二行包含一个整数 mm(1 ≤ m ≤ 2001 \le m \le 200),表示查询的数量。随后 mm 行每行包含一个查询。每个查询是一个序列 x1, x2, ..., xnx_1,\,x_2,\,...,\,x_n,其中 xix_i 是该查询的第 ii 个元素,nn(1 ≤ n ≤ 2001 \le n \le 200)是该查询中元素的个数。各元素之间以单个空格分隔。每个查询既不以空格开头,也不以空格结尾。每个查询元素是由小写拉丁字母组成的字符串,长度为 11 到 1010。

输出格式

Print m lines, the j-th line should contain the number of elements of the document that correspond to the j-th BCSS-rule. If there are no such elements at all, print on the line 0.

输出 m 行,其中第 j 行应包含文档中符合第 j 个 BCSS 规则的元素个数。若不存在任何符合该规则的元素,则在该行输出 0。

输入输出样例

  • 输入#1

    &lt;a&gt;&lt;b&gt;&lt;b&gt;&lt;/b&gt;&lt;/b&gt;&lt;/a&gt;&lt;a&gt;&lt;b&gt;&lt;/b&gt;&lt;b&gt;&lt;v/&gt;&lt;/b&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;
    4
    a
    a b b
    a b
    b a

    输出#1

    2
    1
    4
    0
  • 输入#2

    &lt;b&gt;&lt;aa/&gt;&lt;/b&gt;&lt;aa&gt;&lt;b/&gt;&lt;b/&gt;&lt;/aa&gt;
    5
    aa b
    b
    aa
    b aa
    a

    输出#2

    2
    3
    2
    1
    0

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

首页