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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM