SRM 336 DIV 2 easy & mid

Problem description is somewhat difficult.
Be careful about sentences like "A be followed by B".