21425_LanguageCardinality

2022-5-16 18:19| 发布者: Hocassian| 查看: 41| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505252148499-Problem List-采集的数据-后羿采集器.html

Pro.ID

21425

Title

Language Cardinality

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21425

AC

4

Submit

50

Ratio

8.00%

时间&空间限制

  • Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)
  • 描述

    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.

    输出

    Description

    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.

    Input

    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

    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.

    Sample Input

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

    Sample Output

    9

    Source

    样例输入

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

    样例输出

    9

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部