CF291-C

C. Watto and Mechanism
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 froms 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

给定n个字符串,然后再给m次询问,每次询问包含一个字符串
问当前询问的字符串是否和之前n个字符串中某个字符串只有一个位置的字符不相同
有就输出YES,否则输出NO
很恶心的一题,赛中和赛后WA了20+,只能怪自己智商不够
暴力能过,但是要分类来暴力才能过,想不到
常规方法是:trie树+dfs,正好能练下trie树,就敲了
必须得使用dfs来暴力遍历trie树,这里之前因为没用dfs,wa了10+
dfs过程记录不符字符个数,到根时看是否为1即可
#include <iostream>

using namespace std;

#include <string.h>

char str[600100];

int ok[600100];

int len;

int count;

bool flag;

class TrieTree

{

public:

    TrieTree *next[3];//要初始化!!否则指针会乱指!

    bool isleaf;

    TrieTree()

    {

        isleaf=false;

        for(int i=0;i<3;i++)

            next[i]=NULL;

    }

};

void BuildTree(TrieTree *T,int i)

{

    if(T->next[str[i]-'a']==NULL)

        T->next[str[i]-'a']=new TrieTree();

    if(i==len-1)

    {

        T->next[str[i]-'a']->isleaf=true;

        return;

    }

    BuildTree(T->next[str[i]-'a'],i+1);

}

void dfs(TrieTree *T,int i)

{

    if(flag||count>1)

        return;

    if(i==len)

    {

        if(count==1)

        {

            flag=true;

            cout<<"YES"<<endl;

        }

        return;

    }

    for(int j=0;j<3;j++)

        if(T->next[j]!=NULL)

        {

            if(j!=str[i]-'a')

            {

                count++;

                dfs(T->next[j],i+1);

                count--;

            }

            else

                dfs(T->next[j],i+1);

        }

}

int main()

{

    int n,m;

    cin>>n>>m;

    TrieTree T;

    for(int i=1;i<=n;i++)

    {

        cin>>str;

        len=strlen(str);

        ok[len]=1;

        BuildTree(&T,0);

    }

    for(int i=1;i<=m;i++)

    {

        cin>>str;

        len=strlen(str);

        if(!ok[len])

        {

            cout<<"NO"<<endl;

            continue;

        }

        count=0;

        flag=false;

        dfs(&T,0);

        if(!flag)

            cout<<"NO"<<endl;

    }

    return 0;

}

 

你可能感兴趣的:(c)