A14281 | Funny Substrings
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $5$ 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.
- "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 $5$ 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.
输入格式
The first line contains an integer $t$ ( $1 \le t \le 10^3$ ). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ( $1 \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 $5$ characters.
This is followed by $n$ lines describing the statements in the format described above. It is guaranteed that the program is correct.
The first line of each test case contains a single integer $n$ ( $1 \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 $5$ characters.
This is followed by $n$ lines describing the statements in the format described above. It is guaranteed that the program is correct.
输出格式
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.
输入输出样例
输入 #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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted