Skip Letter Code

翻译 2015年11月19日 21:04:12

Description

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


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is 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 is not longer than 5 characters and (as well as coded message) consists from capital letters A to Z. The rest of file (up to # character) contains coded message (no longer than 10^6 characters) which is coded words separated by space(s) and/or newline characters.


Output



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


Sample Input



2

3
I
LOVE
YOU
I E
U#

3
I
LOVE
YOU
I O
Y#


Sample Output

I LOVE
YOU

AMBIGUITY


这道题是需要你将一个连续的字符串进行解码,注意是连续的,而且是有顺序的,如果对应编码中有

多个答案那么就输出AMBIGUITY,所以需要映射是唯一的才行,可以用组合将编码的所有可能列举出

来,然后采用map存储位置,这样后面就只需要查询了。

#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
map < string, int > vis;
const int N = 105, 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 )   //剪枝
        return ;
    if ( k == n )
    {
        if ( vis[str] == 0 || vis[str] == s )
        //如果里面没数或者相等就赋值
            vis[str] = s;
        else    //否则变成-1表示有多种可能
            vis[str] = -1;
        return ;
    }
    for ( int i = pos; i < len; i ++ )  //组合数要从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';
}
int 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 ++ )   //使用编码的长度
            {
                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'; //注意在后面加换行
            ans[cnt] = '\0';
            if ( op[size-1] == '#' )
                break ;
        }
        printf ( "%s", tag ? "AMBIGUITY\n" : ans );
        if ( T )    //两组数据中有一个换行
            printf ( "\n" );
    }
    return 0;
}


相关文章推荐

使用eclipse编程出现Use View.isInEditMode() in your custom views to skip code when shown in Eclipse解决方法

最近在网上找一些资料demo时查看布局文件出现如下图错误: 只需要在报错的那行代码之间加上if (isInEditMode()) { return; }  然后注意保存就可以解决问题。 还有一种情...

Treap 树堆 和 Skip Lists

  • 2017-07-17 18:11
  • 584KB
  • 下载

skip list算法简介

  • 2009-02-11 13:35
  • 108KB
  • 下载

MYSQL replication slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

建用户的时候报告这个错误,原因是mysql中已经有了这个用户,可以从mysql.user中直接删除,然后刷新权限,再建用户就不会有这个问题了。如果是drop user先,那么mysql内部应该会自动刷...

A skip list cookbook.

  • 2017-02-03 10:18
  • 64KB
  • 下载

跳跃表(Skip List)

跳跃表(Skip List)是1987年才诞生的一种崭新的数据结构,它在进行查找、插入、删除等操作时的期望时间复杂度均为O(logn),有着近乎替代平衡树的本领。而且最重要的一点,就是它的编程复杂度较...

recommendation letter template

  • 2014-12-14 20:01
  • 48KB
  • 下载

A letter to shareholder 2008

  • 2009-06-03 20:33
  • 129KB
  • 下载

跳跃表(Skip List)深入介绍

为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等。   想象一下,给你一张草稿纸,一只笔,一个编辑器,你能立即实现一颗红黑树...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)