GATE Computer ScienceVery hardGATE CS (PYQ pattern)

For the grammar S → SS | SSS | a, how many distinct parse trees does the string aaaaa (five a's) have?

  1. A. 14
  2. B. 42
  3. C. 38
  4. D. 45

Correct answer: C. 38

Let t(n) be the number of trees for aⁿ: t(1) = 1, and the root either splits aⁿ into two nonempty parts (SS) or three (SSS). t(2) = 1, t(3) = 2 + 1 = 3, t(4) = (t1t3 + t2t2 + t3t1) + (3 ways of 1+1+2) = 7 + 3 = 10, t(5) = (t1t4 + t2t3 + t3t2 + t4t1) + Σ over compositions into 3 parts = (10 + 3 + 3 + 10) + (3·t3 + 3·t2·t2·1) = 26 + 12 = 38. (14 is the S → SS | a Catalan count.)

More Theory of Computation questions

Exams that ask this topic