CF1538E.Funny Substrings

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Polycarp came up with a new programming language. There are only two types of statements in it:

  • "x := s": assign the variable named x the value s (where s is a string). For example, the statement var := hello assigns the variable named var the value hello. Note that s is the value of a string, not the name of a variable. Between the variable name, the := operator and the string contains exactly one space each.
  • "x = a + b": assign the variable named x the concatenation of values of two variables a and b. For example, if the program consists of three statements a := hello, b := world, c = a + b, then the variable c will contain the string helloworld. It is guaranteed that the program is correct and the variables a and b were previously defined. There is exactly one space between the variable names and the = and + operators.

All variable names and strings only consist of lowercase letters of the English alphabet and do not exceed 55 characters.

The result of the program is the number of occurrences of string haha in the string that was written to the variable in the last statement.

Polycarp was very tired while inventing that language. He asks you to implement it. Your task is — for given program statements calculate the number of occurrences of string haha in the last assigned variable.

Polycarp 设计了一种新的编程语言。该语言中仅有两类语句:

  • x := s:将字符串 s 的值赋给名为 x 的变量(其中 s 是一个字符串)。例如,语句 var := hello 将字符串 hello 赋给名为 var 的变量。注意,s 是字符串的值,而非变量名。变量名、:= 运算符与字符串之间均恰好有一个空格。
  • x = a + b:将变量 a 与变量 b 的值拼接后所得的字符串赋给名为 x 的变量。例如,若程序包含三条语句 a := hello、b := world、c = a + b,则变量 c 将包含字符串 helloworld。题目保证程序是合法的,即变量 a 和 b 均已在之前定义过。变量名与 =、+ 运算符之间均恰好有一个空格。

所有变量名和字符串均由小写英文字母组成,且长度均不超过 55 个字符。

程序的输出为:在最后一条语句所赋值的变量中,子串 haha 出现的次数。

Polycarp 在设计该语言时非常疲惫。他请你来实现它。你的任务是:对于给定的程序语句,计算最后被赋值的变量中子串 haha 出现的次数。

输入格式

The first line contains an integer tt (1≤t≤1031 \le t \le 10^3). Then tt test cases follow.

The first line of each test case contains a single integer nn (1≤n≤501 \le n \le 50) — the number of statements in the program. All variable names and strings are guaranteed to consist only of lowercase letters of the English alphabet and do not exceed 55 characters.

This is followed by nn lines describing the statements in the format described above. It is guaranteed that the program is correct.

第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3)。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤501 \le n \le 50)—— 表示程序中语句的数量。所有变量名和字符串均保证仅由英文字母的小写字母组成,且长度不超过 55 个字符。

接下来是 nn 行,按上述格式描述各条语句。保证程序是正确的。

输出格式

For each set of input data, output the number of occurrences of the haha substring in the string that was written to the variable in the last statement.

对于每组输入数据,输出最后一条语句中写入变量的字符串中子串 haha 出现的次数。

输入输出样例

  • 输入#1

    4
    6
    a := h
    b := aha
    c = a + b
    c = c + c
    e = c + c
    d = a + c
    15
    x := haha
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    x = x + x
    1
    haha := hah
    5
    haahh := aaaha
    ahhhh = haahh + haahh
    haahh = haahh + haahh
    ahhhh = ahhhh + haahh
    ahhaa = haahh + ahhhh

    输出#1

    3
    32767
    0
    0

说明/提示

In the first test case the resulting value of d is hhahahaha.

在第一个测试用例中,d 的最终值为 hhahahaha。

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

首页