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

用字典树解决,渣渣不熟悉字典树,写在这里以后多看看。。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 601005
#define MAXN 3
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
using namespace std;

typedef struct TrieNode
{
    int nCount; //标记该结点是否是某个字符串的结尾
    struct TrieNode *next[MAXN];
}TrieNode;

char s[maxn];
int n,m;
TrieNode *proot;

TrieNode *CreateTrieNode()
{
    TrieNode *p;
    p=(TrieNode *)malloc(sizeof(TrieNode));
    p->nCount=0;
    for (int i=0;i<3;i++)
        p->next[i]=NULL;
    return p;
}

void Trie_insert(char *s)  //建字典树
{
    TrieNode *p;
    p=proot;
    if (!p)
        p=proot=CreateTrieNode();
    int i=0;
    while (s[i])
    {
        int k=s[i++]-'a';
        if (!p->next[k])
            p->next[k]=CreateTrieNode();
        p=p->next[k];
    }
    p->nCount=1;
}

bool dfs(TrieNode *root,int len,int flag)  //dfs搜索
{
    if (s[len])
    {
        int k=s[len]-'a';
        if (root->next[k]!=NULL)
        {
            if (dfs(root->next[k],len+1,flag))
                return true;
        }
        if (!flag)
        {
            for (int i=0;i<3;i++)
                if (i!=k&&root->next[i]!=NULL)
                    if (dfs(root->next[i],len+1,++flag))
                        return true;
        }
    }
    else
    {
        if (flag&&root->nCount==1)
            return true;
    }
    return false;
}

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        proot=NULL;
        for (int i=0;i<n;i++)
        {
            scanf("%s",s);
            Trie_insert(s);
        }
        for (int i=0;i<m;i++)
        {
            scanf("%s",s);
            if (dfs(proot,0,0)) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}
时间: 2024-08-26 16:41:08

C. Watto and Mechanism 字典树 Codeforces Round #291 (Div. 2)的相关文章

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

set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

题目传送门 1 /* 2 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 3 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 4 查找左右相邻的位置,更新长度为r - l - 1的最大值,感觉线段树结构体封装不错! 5 详细解释:http://blog.csdn.net/u010660276/article/details/46045777 6 其实还有其他解法,先掌握这种:) 7 */ 8 #include <cstd

线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

题目传送门 1 /* 2 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 3 详细解释:http://www.xuebuyuan.com/1154895.html 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <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 Round #291 (Div. 2)解题报告A.B.C.D

A - Chewbaсca and Number 大于4的倒置,小于等于4的不倒置.注意第一位如果是9则不倒置. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set>

Codeforces Round #291 (Div. 2)

A 题意:给出变换规则,单个数字t可以变成9-t,然后给出一个数,问最小能够变成多少. 自己做的时候理解成了不能输出前导0,但是题目的本意是不能有前导0(即最高位不能是0,其余位数按照规则就好) 555555---读题仔细o(╯□╰)o 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 char

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

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) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(