Parrots
Time limit1sMemory limit512 MB
Given N parrot sentences and one written sequence, decide whether distinct words can interleave so that each parrot's words stay in order and no word repeats.
- Level
Medium7 of 10
- Topics
- Simulation, Greedy, Hash map, Implementation
- Solved
- No attempts yet
Problem
While flying around the world in their private plane, pps789 and cseteram lost an engine and crash landed on an unnamed island. Exploring it, they learned that the parrots living there imitate human speech remarkably well. The two split up to explore separately and agreed to use the parrots if they had to contact each other.
A month later pps789 found decisive evidence about the secret of the island. He wanted to share the discovery with cseteram, but it was far too long for a single parrot to memorize. So pps789 split the discovery across parrots and sent them flying to cseteram.
On the other side of the island, cseteram was startled when parrots arrived and each started talking. He realized that pps789 was sending a long message, but he wrote the words down in the order he heard them, and the order came out so tangled that the original text was lost. What he did work out, by watching the parrots, were a few rules.
- Each parrot memorizes one sentence. A sentence is made of several words, and the parrot says those words in the memorized order.
- After a parrot says a word and before it says the next one there is a short gap, and during that gap another parrot can cut in and say its own sentence.
- While a parrot is in the middle of saying a word, no other parrot cuts in.
- No word appears twice across all of the sentences the parrots say.
Each parrot says its memorized sentence to the end and then flies back to pps789, and cseteram writes down words until every parrot has left. You are given the sentence that pps789 gave to each parrot and the sentence that cseteram wrote down. Decide whether can come out under the rules above.
Input
The first line contains the number of parrots ().
Each of the next lines contains the sentence () that one parrot says, one sentence per line. The words of a sentence are separated by a single space. Sentence has between 1 and 100 words, and each word consists of 1 to 32 lowercase English letters.
Line contains the sentence that cseteram wrote down. has between 1 and 10000 words, and each word consists of 1 to 32 lowercase English letters.
Output
Print Possible if the sentence can come out, and Impossible otherwise.