Skip Letter Code

Source: Internet
Author: User
Tags int size strlen

Description

Romeo and Juliet is college students in the city of Verona and is in love with each other. Unfortunately, both their families is not very glad with this fact. They is trying to prevent any attempts of young people to communicate. Thus, Romeo and Juliet had to introduce their special code to being capable of writing love letters to each other. Both of them has a little book of Shakespeare sonnets and agreed to use the this book as a dictionary for their code. The code is like follows:only words, appear in the dictionary, be used in the love letter. Then, the "who writes" The letter may skip some letters in each word. Of course that person tries to skip as could letters as possible, but he would keep on mind that the letter should be decode D with just one possibility for each word. Oh, such a difficult task! Fortunately, both of the lovers has a personal computer and you can (can ' t ") help them writing the program, that Bein G Given with the dictionary and the coded message DEcodes One if there is just one-to-decode the message or reports that there could being more than one decoding Possibilit Y. Thus, you'll help both writing and reading persons.


This problem contains multiple test cases!

The first line of a multiple input was an integer N and then a blank line followed by N input blocks. Each input block was in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

First line of the input contains single integer 1 <= N <= 100-number of words in a dictionary. Next N lines each contain one word from the dictionary. Each word in a dictionary are not longer than 5 characters and (as well as coded message) consists from capital letters a T o Z. The rest of file (up to # character) contains coded message (no longer than 10^6 characters) which is coded words separate D by Space (s) and/or newline characters.


Output

Output should contain single line with word ambiguity if there could being more than one possible decoding of the message or Decoded message with all spaces and newline characters kept intact. Note, that # character should is printed.


Sample Input

2

3
I
Love
You
I E
u#

3
I
Love
You
I O
y#


Sample Output

I Love
You

Ambiguity


The problem is that you need to decode a contiguous string, note that it is sequential, and is sequential if the corresponding encoding has

Multiple answers then output ambiguity, so the mapping is unique, and you can use the combination to enumerate all the possible lists of encodings

, and then use the map storage location so that you only need to query it later.

#include <stdio.h> #include <string.h> #include <string> #include <map> #include <iostream
> Using namespace std;
Map < string, int > vis;
const int N = MAXN, = 1000005;
Char Word[n][n], op[n], ANS[MAXN];
int S, Len;
    void Dfs (int k, string str, int n, int pos) {if (k > N | | len-pos < N-K)//pruning return;
        if (k = = N) {if (vis[str] = = 0 | | vis[str] = = s)//If there is no number or equality in the assignment vis[str] = s;
        else//Otherwise-1 means there are multiple possible vis[str] = 1;
    return;
} for (int i = pos; i < len; i + +)//combination number to start with i+1 Dfs (k+1, str+word[s][i], N, i+1); } int is_letter (char ch) {return ch >= ' a ' && ch <= ' z ' | | ch >= ' a ' && ch <= ' z ';} i
    NT main () {int T, n, tag, CNT;
    Freopen ("In0.in", "R", stdin);
    scanf ("%d", &t);
    String str;
        while (T--) {tag = 0;
        Vis.clear (); scanf ("%d", &n);
        for (int i = 1; I <= n; i + +) scanf ("%s", Word[i]);
            for (int i = 1; I <= n; i + +) {str = "";
            Len = strlen (Word[i]);
                for (int j = 1; j <= Len; j + +)//Use encoded length {s = i;
            DFS (0, str, j, 0);
        }} getchar ();
        strcpy (ans, "");
        CNT = 0;
            while (gets (OP)) {int size = strlen (OP);
                if (tag) {if (op[size-1] = = ' # ') break;
            Continue;
                } for (int i = 0; i < size; I + +) {string ch = "";
                        if (Is_letter (op[i))) {while (Is_letter (Op[i])) {
                        ch = ch+op[i];
                    i + +; } I--;
                    if (vis[ch] = =-1 | | vis[ch] = = 0) tag = 1;
                        else {int pos = vis[ch];
                        int L = strlen (Word[pos]);
                    for (int i = 0; i < l; i + +) ans[cnt + +] = Word[pos][i];
            }} else if (op[i]! = ' # ') ans[cnt +] = op[i]; } ans[cnt + +] = ' \ n ';
            Note the trailing line ans[cnt] = ' + ';
        if (op[size-1] = = ' # ') break; } printf ("%s", tag?)
        "ambiguity\n": ans);
    if (T)//Two group data has a newline ("\ n");
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.