当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

3220:Context-Free Languages

题目描述

Context-free grammars (CFG) are a powerful method of describing languages. The syntax of most programming languages including C, C++, Java and Pascal, compilers for which are provided in this online judge system, is specified using CFGs.

In this problem, we work with CFGs in a generative manner. A CFG consists of a set of production rules for transforming strings. Every rule is of the form

Vw

where V is a symbol and w is a string of symbols. Symbols are classified into terminals and variables. In the rule above, V is restricted to be a variable, while w can contain terminals and/or variables. The term “context-free” expresses the fact that V can always be replaced by w, regardless of the context in which it occurs. Among variables, exactly one of them is designated as the start variable. To generate a string using the CFG, one begins with a string consisting of only a single start variable, and then applies the rules successively and arbitrarily to rewrite the string until only terminals are left.

For example, the alphabet of terminals consists of z, the start variable is S and we we have the following rules:

  1. SCB
  2. SZZ
  3. ACB
  4. AZZ
  5. BZZ
  6. CBA
  7. Zz

then we start with S, and we can choose a rule to apply to it. If we choose rule 1, we replace S with CB and obtain the string CB. If we then choose rule 6, we replace C with BA and obtain the string BAB. If we now choose rule 4, we replace A with ZZ and obtain the string BZZB. We can write this series of choice more briefly, using symbols: SCBBABBZZBZZZZBZZZZZZzZZZZZzzZZZZzzzZZZzzzzZZzzzzzZzzzzzz. The language of the grammar is the set of all strings consisting of twice an odd number of z’s.

Given a CFG and some strings, determine whether each string belongs to the language of the grammar.

输入解释

To make everything brief, we group together all rules with the same variable to the left of “→”  in the CFG. For example, we group the following three rules

Su
Sv
Sw

into

Su | v | w

And the CFG is given in a special form called Chomsky normal form (CNF). A CFG in CNF only contains rules of the form

ABC
Aa

where a is any terminal and A, B, and C are any variables except that B and C may not be the start variable. CNF also permits the rule Sε, where S is the start variable and ε represents the empty string, so that the CFG can generate the empty string. We ignore this case just for this problem.

The input contains exactly one CFG in CNF and no more than 50 strings. We use single lowercase letters as terminals and single uppercase letters as variables. S will always be the start variable. The CFG is given in several lines. A line contains all production rules for one variable grouped together as described above. “→” will be replaced by “->”. Not all possible variables are involved in the CFG, but every involved one will has at least one production rule. The CFG ends with a line containing only a single “#”. Following the CFG come the strings, one on a separate line. The input ends where no more strings can be found. Spaces and blank lines will not show up in the input.

输出解释

For each string, output exactly one line. If the string belongs to the language of the given CFG, the line should read “YES” (without quotes); otherwise it should read “NO” (without quotes).

输入样例
S->CB|ZZ
A->CB|ZZ
B->ZZ
C->BA
Z->z
#
zzzzzz
z
a
输出样例
YES
NO
NO

该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-3220

最后修改于 2020-10-29T06:56:08+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
2000 131072