Trie(Codeforces Round #291 (Div. 2))

来源:互联网 发布:java条形码生成代码 编辑:程序博客网 时间:2024/05/19 01:13

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 withn strings. Then the mechanism should be able to process queries of the following type: "Given strings, determine if the memory of the mechanism contains stringt that consists of the same number of characters ass and differs from s 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 ofn initial lines and m queries. He decided to entrust this job to you.

Input

The first line contains two non-negative numbers n andm (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 consistsonly 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


思路:构建trie树后,每次判断,对每个位置枚举每种字母替换(因为只有三种字母),然后进行判断

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=1000010;const int SIGMA_SIZE=3;int N,M;char str[maxn];struct Trie{    int ch[maxn][4];    int val[maxn];    int sz;    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}    int idx(char x){return x-'a';}    void insert(char *s,int v)    {        int u=0;        int n=strlen(s);        for(int i=0;i<n;i++)        {            int c=idx(s[i]);            if(!ch[u][c])            {                memset(ch[sz],0,sizeof(ch[sz]));                val[sz]=0;                ch[u][c]=sz++;            }            u=ch[u][c];        }        val[u]=v;    }    bool solve(char *s)    {        int u=0;        int n=strlen(s);        for(int i=0;i<n;i++)        {            int c=idx(s[i]);            for(int j=0;j<SIGMA_SIZE;j++)            {                if(c==j||ch[u][j]==0)continue;                int u2=ch[u][j];                bool flag=true;                for(int k=i+1;k<n;k++)                {                    int c2=idx(s[k]);                    if(!ch[u2][c2])                    {                        flag=false;                        break;                    }                    u2=ch[u2][c2];                }                if(flag&&val[u2])return true;            }            if(!ch[u][c])return false;            u=ch[u][c];        }        return false;    }}tree;int main(){    while(scanf("%d%d",&N,&M)!=EOF)    {        tree.clear();        for(int i=1;i<=N;i++)        {            scanf("%s",str);            tree.insert(str,1);        }        while(M--)        {            scanf("%s",str);            if(tree.solve(str))printf("YES\n");            else printf("NO\n");        }    }    return 0;}




0 0