1143. Language Cardinality

单点时限: 2.0 sec

内存限制: 256 MB

A (formal) language is a set of strings. One way to define a particular langauge is using ordinary set notation. Alternatively, some form of grammar may be more convenient for representing large sets. The UW grammar in which we are interested has two parts:

An initial string

A set of replacement rules of the form s1 -> s2 where s1 and s2 are strings

The language defined by this grammar is the set of all strings that can be generated by repeatedly replacing s1 by s2 within the initial string. For example, consider the grammar G consisting of the initial string

“AyB”

and the replacement rules

{“A”->”ab”, “Ay”->”cdy”, “B”->”w”, “B”->”x”} .

G generates the language

L = {“AyB”, “Ayw”, “Ayx”, “abyB”, “abyw”, “abyx”, “cdyB”, “cdyw”, “cdyx”}

Given a UW grammar G, compute how many different strings there are in the language generated by G.

输入格式

The first line of input contains the initial string. The second and subsequent lines contain the replacement rules, one per line, terminated by end-of-file. There are at most 100 replacement rules. Each input string contains between 0 and 10 upper and lower case letters, and is enclosed in quotes. There are no spaces in the input.

输出格式

Output consists of a single integer, the number of distinct strings in the language generated by G. If there are more than 1000 distinct strings, print “Too many.” instead.

样例

Input
"AyB"
"A"->"ab"
"Ay"->"cdy"
"B"->"w"
"B"->"x"
Output
9

4 人解决,5 人已尝试。

5 份提交通过,共有 11 份提交。

7.3 EMB 奖励。

创建: 17 年,2 月前.

修改: 6 年,10 月前.

最后提交: 3 年,7 月前.

来源: partychen

题目标签