Codeforces517C——哈希字符串——Watto and Mechanism

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 3aaaaaacacacaaabaaccacacccaaac

output

YESNONO
/*
显然要用nlogn的方法,用set的STL
对于每一个串都有一个特定的哈希值,改变某个值,就是看这个哈希值是否已经出现过。
设一个好的KEY防止冲突
*/
/************************************************
Author        :powatr
Created Time  :2015-8-5 18:28:51
File Name     :c.cpp
************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAX = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int KEY = 257;//ORZ
int n, m;
set <ll> s;
char s1[MAX];
char s2[MAX];
ll f[MAX];

void inti()
{
    f[0] = 1;
    for(int i = 1; i <= MAX; i++){
        f[i] = f[i-1] * KEY % MOD;
    }
}

ll Hash(char *s1)
{
    int len = strlen(s1);
    ll tmp = 0;
    for(int i = 0 ; i < len; i++){
        tmp = (tmp * KEY + s1[i]) % MOD;
    }
    return tmp;
}

bool check(char *s2)
{
    int len = strlen(s2);
    ll h = Hash(s2);
    for(int i = 0 ; i < len; i++){
        for(ll ch = ‘a‘; ch <= ‘c‘; ch++){
            if(ch == s2[i]) continue;
            if(s.find((((ch - s2[i]) * f[len - i - 1] + h)%MOD + MOD)%MOD) != s.end())
                return true;
        }
    }
    return false;
}
int main()
{
    inti();
    while(~scanf("%d%d", &n, &m)){
        s.clear();
        for(int i = 1; i <= n; i++){
            scanf("%s", s1);
            s.insert(Hash(s1));
        }

        for(int i = 1; i <= m; i++){
            scanf("%s", s2);
            puts(check(s2) ? "YES" : "NO");
        }
    }
    return 0;
}

  

时间: 2024-10-26 11:52:27

Codeforces517C——哈希字符串——Watto and Mechanism的相关文章

hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

题目传送门 1 /* 2 hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 3 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用64位的自然溢出wa了,不清楚 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 13:05:49 8 * File N

C. Watto and Mechanism 字典树 Codeforces Round #291 (Div. 2)

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 ce

Codeforces 514C Watto and Mechanism(字典树)

题目链接  Watto and Mechanism 题意  给出n个串(相当于字典),然后给出m个询问. 每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符 (必须改变且只能改变一个) 如果改变之后可以成为n个串中的一个字符串,则输出YES, 否则输出NO. 字母集合为{a, b, c} 考虑字典树. 首先把n个单词插入字典树中. 询问的时候用dfs,flag表示搜索到当前是否已经改变过一个字符. 如果已经改变过那只能按照当前剩下的字符串一条路查询下去. 否则可以按老字符或新字符进

codeforces 514C Watto and Mechanism (分段暴力)

codeforces 514C Watto and Mechanism (分段暴力) 题意: 给出一个包含n个单词的字典,给出m个待查询单词,如果单词在有且仅有一个字符不相同的情况下可以在字典里找到,则输出YES,否则输出NO 限制: 0 <= n,m <= 3*10^5; 总字符长度不大于6*10^5 思路: 分段暴力. 以查询单词长度为500分段: 查询单词长度<500则:采用set查询,复杂度为600*500*500=1.5*10^8 查询单词长度>500则:暴力查询,复杂度

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

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 ce

PHP处理0e开头md5哈希字符串缺陷/bug

PHP在处理哈希字符串时,会利用”!=”或”==”来对哈希值进行比较,它把每一个以”0E”开头的哈希值都解释为0,所以如果两个不同的密码经过哈希以后,其哈希值都是以”0E”开头的,那么PHP将会认为他们相同,都是0. 关于PHP hash比较缺陷详细介绍:http://www.freebuf.com/news/67007.html 0x01 md5(str) QNKCDZO 0e830400451993494058024219903391 s878926199a 0e54599327451770

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

[题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个字符不同.(字符串总长度为6·105),所有字符只有a,b,c. [题解]因为只有三种字符,用Trie最合适.先把n个字符串插入到Trie中.然后每读入一个字符串,首先枚举差异字符的位置,依次替换为另外两种字符,并检查是否合法.如果合法就直接输出YES. 显然每替换一个字符就从头检查一遍太浪费时间,

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

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 th

tries树第一题 codeforces 514C - Watto and Mechanism

题目链接 题意: 输入a个已知字符串和b个待检测字符串.问待检测字符串是否可以由某个已知字符串改变且只改变一个字母得到.可以输出YES,否则NO. 思路: 用trie树储存已知字符串.dfs改变一个字母看能否匹配. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> using namespace std;