F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Obfuscation

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 517    Accepted Submission(s): 146


Problem Description
It is a well-known fact that if you mix up the letters of a word, while leaving the first and last letters in their places, words still remain readable. For example, the sentence ¡°tihs snetncee mkaes prfecet sesne¡±, makes perfect sense to most people.

If you remove all spaces from a sentence, it still remains perfectly readable, see for example: ¡°thissentencemakesperfectsense¡±, however if you combine these two things, first shuffling, then removing spaces, things get hard. The following sentence is harder to decipher: ¡°tihssnetnceemkaesprfecetsesne¡±.

You¡¯re given a sentence in the last form, together with a dictionary of valid words and are asked to decipher the text.

 

Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with a string s: the sentence to decipher. The sentence consists of lowercase letters and has a length of at least 1 and at most 1 000 characters.

One line with an integer n with 1 ¡Ü n ¡Ü 10 000: the number of words in the dictionary.

n lines with one word each. A word consists of lowercase letters and has a length of at least 1 and at most 100 characters. All the words are unique.
 

Output
Per testcase:

One line with the deciphered sentence, if it is possible to uniquely decipher it. Otherwise ¡°impossible¡± or ¡°ambiguous¡±, depending on which is the case.
 

Sample Input
3 tihssnetnceemkaesprfecetsesne 5 makes perfect sense sentence this hitehre 2 there hello hitehre 3 hi there three
 

Sample Output
this sentence makes perfect sense impossible ambiguous
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-04-27 00:24:09, Gzip enabled