码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #291 (Div. 2)---C. Watto and Mechanism

时间:2015-02-15 15:14:32      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:trie

Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: “Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position”.

Watto has already compiled the mechanism, all that’s left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
Input

The first line contains two non-negative numbers n and m (0?≤?n?≤?3·105, 0?≤?m?≤?3·105) — the number of the initial strings and the number of queries, respectively.

Next follow n non-empty strings that are uploaded to the memory of the mechanism.

Next follow m non-empty strings that are the queries to the mechanism.

The total length of lines in the input doesn’t exceed 6·105. Each line consists only of letters ‘a’, ‘b’, ‘c’.
Output

For each query print on a single line “YES” (without the quotes), if the memory of the mechanism contains the required string, otherwise print “NO” (without the quotes).
Sample test(s)
Input

2 3
aaaaa
acacaca
aabaa
ccacacc
caaac

Output

YES
NO
NO

trie + dfs

/*************************************************************************
    > File Name: cf291-c.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月15日 星期日 12时16分59秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 7 * 100010;
char str[N];
int has_len[N];
int len;

struct TRIE
{
    TRIE *next[3];
    int cnt;
    TRIE ()
    {
        cnt = 0;
        next[0] = next[1] = next[2] = NULL;
    }
}*root;

void insert (char str[])
{
    int len = strlen (str);
    TRIE *p = root;
    for (int i = 0; i < len; ++i)
    {
        if (p -> next[str[i] - ‘a‘] == NULL)
        {
            p -> next[str[i] - ‘a‘] = new TRIE ();
        }
        p = p -> next[str[i] - ‘a‘];
    }
    p -> cnt = 1;
}

bool find (TRIE *p, int cur, bool has)
{
    if (cur == len)
    {
        return has && p -> cnt;
    }
    bool flag = 0;
    for (int i = 0; i < 3; ++i)
    {
        if (p -> next[i] != NULL)
        {
            if (i == str[cur] - ‘a‘)
            {
                flag |= find (p -> next[i], cur + 1, has);
            }
            else
            {
                if (has)
                {
                    continue;
                }
                flag |= find (p -> next[i], cur + 1, 1);
            }
        }
    }
    return flag;
}

void DELETE (TRIE *p)
{
    for (int i = 0; i < 3; ++i)
    {
        if (p -> next[i] != NULL)
        {
            DELETE (p -> next[i]);
        }
    }
    delete p;
}

int main ()
{
    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        memset (has_len, 0, sizeof(has_len));
        root = new TRIE();
        for (int i = 0; i < n; ++i)
        {
            scanf("%s", str);
            len = strlen (str);
            has_len[len] = 1;
            insert (str);
        }
        while (m--)
        {
            scanf("%s", str);
            len = strlen (str);
            if (!has_len[len])
            {
                printf("NO\n");
                continue;
            }
            if (find (root, 0, 0))
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        DELETE(root);
    }
    return 0;
}

Codeforces Round #291 (Div. 2)---C. Watto and Mechanism

标签:trie

原文地址:http://blog.csdn.net/guard_mine/article/details/43834575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!