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

本文介绍了一种用于处理字符串查询的机制实现方法。该机制能够高效地判断预存字符串集合中是否存在与给定字符串长度相同且仅一位不同的字符串。通过构建Trie树数据结构,并结合深度优先搜索算法,实现了对字符串集合的有效查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值