Codeforces 514C Watto and Mechanism Trie树 + dfs

9 篇文章 0 订阅
9 篇文章 0 订阅

题目大意:

现在需要找的字符串S的T的关系为两者长度相同且刚好有1处字符不同

就是对于给定的n个字符串(n <= 3*10^5), 对于接下来输入的m次字符串询问(m <= 3*10^5), 每一次询问的字符串如果在给定的n个字符串中存在满足上述关系的串输出YES, 否则输出NO


大致思路:

就是将n个字符串插入Trie树暴搜一下就过了....

给出一点可能错的样例吧:

Input:

3 2

a

b

c

bb

a

b


Output:

NO

YES

YES


代码如下:

Result  :  Accepted     Memory  :  255196 KB     Time  :  358 ms

/*
 * Author: Gatevin
 * Created Time:  2015/2/25 13:24:49
 * File Name: poi~.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
int n, m, N;
char in[800000];
const int max_node = 20000000;//10000000 Runtime Error果断改20000000了...
bool flag;
struct Trie
{
    int next[max_node][3];
    bool end[max_node];
    int L, root;
    int newnode()
    {
        for(int i = 0; i < 3; i++)
            next[L][i] = -1;
        end[L++] = 0;
        return L - 1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char *s)
    {
        int now = root;
        for(; *s; s++)
        {
            if(next[now][*s - 'a'] == -1)
                next[now][*s - 'a'] = newnode();
            now = next[now][*s - 'a'];
        }
        end[now] = 1;
    }
    void dfs(int len, bool used, int now)
    {
        if(len == N && used && end[now]) flag = true;
        if(flag) return;
        if(len >= N) return;
        if(used)
        {
            if(next[now][in[len] - 'a'] != -1)
                dfs(len + 1, used, next[now][in[len] - 'a']);
        }
        else
        {
            for(int i = 0; i < 3; i++)
                if(len + 1 == N && next[now][i] != -1 && i != in[len] - 'a' && end[next[now][i]])
                    flag = true;
            for(int i = 0; i < 3; i++)
                if(i != in[len] - 'a' && next[now][i] != -1)
                    dfs(len + 1, true, next[now][i]);
            if(next[now][in[len] - 'a'] != -1)
                dfs(len + 1, false, next[now][in[len] - 'a']);
        }
    }
};

Trie trie;

int main()
{
    scanf("%d %d", &n, &m);
    trie.init();
    while(n--)
    {
        scanf("%s", in);
        trie.insert(in);
    }
    while(m--)
    {
        scanf("%s", in);
        N = strlen(in);
        flag = false;
        trie.dfs(0, false, 0);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值