Codeforces C. Watto and Mechanism (字典树+DFS)

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86518484

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 from sin 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).

Examples

input

Copy

2 3
aaaaa
acacaca
aabaa
ccacacc
caaac

output

Copy

YES
NO
NO

题目大意:输入n 个字符串,有m次询问,每次询问输入一个字符串,要求判断每次询问的字符串在n个字符串中是否存在一个字符串最多只有一个字符不同的字符串。若存在输出YES, 否则输出NO。

解题思路:字典树+DFS,把输入的n个字符串存到字典树中,对于每次的询问,只需要在字典树中深度优先搜索一下就可以了,如果有与本身相同的字符串返回true, 或者有与自身仅有一个字符不同的也输出true, 还有就是在对于询问的字符串搜索完成后,应该有一个字符不同并且是一个完整的字符串也返回true即可。具体看代码很容易懂!

在建字典树时用递归建树也可以,非递归也可以,体现在代码里,具体看代码!

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 3e5 +10 ;
const int Max = 3 ;

struct Tire{
    Tire *Next[Max] ;
    int mark ;
    Tire(){
        memset(Next, NULL, sizeof(Next)) ;
        mark = 0 ;
    }
};

int n, m ;
string s ;
Tire *root ;

//void insert (){
//    Tire *p = root ;
//    for (auto i : s){
//        int id = i - 'a' ;
//        if (p -> Next[id] == 0){
//            p -> Next[id] = new Tire() ;
//        }
//        p = p -> Next[id] ;
//    }
//    p -> mark = 1 ;
//}

void insert1 (Tire *p, int len){
    if (s[len]){
        int id = s[len] - 'a' ;
        if (p -> Next[id] == 0){
            p -> Next[id] = new Tire() ;
        }
        insert1(p -> Next[id], len + 1) ;
    }
    else p -> mark = 1 ;
}

bool Dfs(Tire *p, int len, int cnt){
    if (s[len]){
        int id = s[len] - 'a' ;
        if (p -> Next[id] != 0){
            if (Dfs(p -> Next[id], len + 1, cnt)) return true ;
        }
        if (!cnt){
            for (int i = 0; i < 3; i++){
                if (i != id && p -> Next[i] != 0){
                    if (Dfs(p -> Next[i], len + 1, cnt + 1)) return true ;
                }
            }
        }
    }
    else {
        if (cnt && p -> mark == 1 ) return true ;
    }
    return false ;
}

int main (){
    cin >> n >> m ;
    root = new Tire() ;
    for (int i = 1; i <= n; i++){
        cin >> s ;
//        insert() ;
        insert1(root, 0) ;
    }
    for (int i = 1; i <= m; i++){
        cin >> s ;
        if (Dfs(root, 0, 0)) cout << "YES" << endl ;
        else cout << "NO" << endl ;
    }
    return 0 ;
}

自在飞花轻似梦

无边丝雨细如愁

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86518484