zoukankan      html  css  js  c++  java
  • Codeforce 633.C Spy Syndrome 2

    C. Spy Syndrome 2
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.

    For a given sentence, the cipher is processed as:

    1. Convert all letters of the sentence to lowercase.
    2. Reverse each of the words of the sentence individually.
    3. Remove all the spaces in the sentence.

    For example, when this cipher is applied to the sentence

    Kira is childish and he hates losing

    the resulting string is

    ariksihsidlihcdnaehsetahgnisol

    Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of nlowercase English letters — the ciphered text t.

    The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.

    Output

    Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.

    Examples
    input
    30
    ariksihsidlihcdnaehsetahgnisol
    10
    Kira
    hates
    is
    he
    losing
    death
    childish
    L
    and
    Note
    output
    Kira is childish and he hates losing 
    input
    12
    iherehtolleh
    5
    HI
    Ho
    there
    HeLLo
    hello
    output
    HI there HeLLo 
    Note

    In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.

    题目大意:将一个字符串加密的规则:先将所有字母变成小写字母,再将每个单词翻转,拼接在一起.现在给出可能用到的单词,还原字符串.

     分析:既然题干中说所有的单词都翻转过来了,那么就把它给出的单词全部翻转过来.之后就有点像是在一个字典中查询单词有没有出现过这种操作,利用trie.因为n不大,在匹配加密串的时候可以用搜索:固定起点,枚举终点,每次看在trie中能不能找到结尾标记以及能不能走下去.翻转操作可以变成倒着插入trie.

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n, m, tot = 1, cnt, ans[100010], len[100010];
    char s[100010][1010], s2[10010];
    
    struct node
    {
        int tr[30];
        int id;
    }e[1000010];
    
    void insert(char *ss, int x)
    {
        int len = strlen(ss);
        int u = 1;
        for (int i = len - 1; i >= 0; i--)
        {
            char ch = ss[i];
            if (ch < 'a' || ch > 'z')
                ch += 'a' - 'A';
            int p = ch - 'a';
            if (!e[u].tr[p])
                e[u].tr[p] = ++tot;
            u = e[u].tr[p];
        }
        e[u].id = x;
    }
    
    void solve(int dep)
    {
        if (dep == n + 1)
        {
            for (int i = 1; i < cnt; i++)
                cout << s[ans[i]] << " ";
            cout << s[ans[cnt]] << endl;
            exit(0);
        }
        int u = 1,i;
        for (i = dep; i <= n; i++)
        {
            int p = s2[i] - 'a';
            if (!e[u].tr[p])
                break;
            u = e[u].tr[p];
            if (e[u].id)
            {
                ans[++cnt] = e[u].id;
                solve(dep + len[e[u].id]);
                --cnt;
            }
        }
    }
    
    int main()
    {
        scanf("%d", &n);
        scanf("%s", s2 + 1);
        scanf("%d", &m);
        for (int i = 1; i <= m; i++)
        {
            scanf("%s", s[i]);
            len[i] = strlen(s[i]);
            insert(s[i], i);
        }
        solve(1);
    
        return 0;
    }
  • 相关阅读:
    poj1904 King's Quest
    ACM竞赛须掌握的知识 以及 2个版本的POJ推荐 @ NKOJ discuss 转载的
    poj1466
    C++23中设计模式的factory模式
    poj3667 hotel
    poj1505 Copying Books
    在linux系统中安装VSCode(Visual Studio Code)
    Spring_的jar详细说明
    java开发问题总结4Maven使用问题汇总
    线程同步之信号量(sem_init,sem_post,sem_wait)
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8051622.html
Copyright © 2011-2022 走看看