The Grand Adventure
Our hero, Jim, is about to embark on his grand adventure. On his way, he’ll encounter three different types of objects: money, incense, and gems. When he encounters an object, he will always put it into his backpack. Unfortunately, he’ll also encounter villains along the way. More specifically, the Banker (who will demand money), the Trader (who demand incense), and the Jeweler (who will demand, of course, jewels). Jim must give each villain one of the kind of item they demand as he encounters them, or else he fails and his adventure is over. Unfortunately, Jim’s backpack isn’t very ergonomic and so he can only reach the item he most recently put in it. In other words, the items below are inaccessible until he’s given away the one on top. Jim would also like arrive at his destination with no extra items in his bag. If he does have items at the end, he’ll consider his adventure a failure.
We’d like to know if Jim will be able to complete his adventure before he even starts!
Input
The first line contains a single integer n, 1≤ n ≤ 5, the number of adventures. Each of the next n lines contains a single string a, a sequence of {$,∗,|,t,j,b,.}{$,∗,|,t,j,b,.}. An example of this adventure string is:
....$...$...*..*...|...t...j..j...b..b...
where,
- $ represents Money
- | represents Incense
- * represents Gem
- t represents a Trader
- j represents a Jeweler
- b represents a Banker
- . represents the Ground (nothing)
and 1 ≤ |a| ≤ 100 where |a| is the length of string a. There is no limit on the number of items Jim may place in his backpack.
Output
Print YES if Jim is able to finish his adventure and print NO if he is unable to.
Sample 1
Input:
5
.$.b.|.t.*.j.................$$$$$bbbbb||....tt.....
.$.b.|.t.*.j..
................................
.......$....$......*....*.....|......t........j...j.....b..b........
...$.$.$..*..*..*...*..|..*..b.....*******...
Output:
YES
YES
YES
YES
NO
Sample 2
Input:
4
........$b...$$..t...*..*...j.........j...
.........*****jjjj...............|tj....
.$.|.*.$.|.*.$.|.*.j.t.b.j.t.b.j.t.b.
...$$..$$..$$..|..$$..b......b....t..bbbbbb.....j...
Output:
No
YES
YES
NO
This is the Kattis problem The Grand Adventure.