Problem 1884 --Problem B: Language Cardinality

1884: Problem B: Language Cardinality

"
Time Limit $1$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $2$ 正确数量 $2$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签

Problem B: Language Cardinality

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.

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

推荐代码 查看1884 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$28 $ms] AOJ大管家 84399 2017-04-27 15:28:11
内存最少[$0 $KB] 淡意的温柔 588603 2020-05-29 15:22:28
第一AC AOJ大管家 84399 2017-04-27 15:28:11
第一挑战 AOJ大管家 84399 2017-04-27 15:28:11

赛题来源/所属竞赛 NA N/A

竞赛编号 竞赛名称 竞赛时间 访问比赛