码迷,mamicode.com
首页 > 其他好文 > 详细

CODEVS 4189 (前缀是否出现)

时间:2019-08-10 09:24:15      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:http   ret   pre   dev   include   eof   typedef   前缀   std   

题目链接:http://codevs.cn/problem/4189/

#include <iostream>
#include <cstring>
#include <cstdio>
#define mem(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
const int maxn = 500005;
const ll INF = 0x3f3f3f3f;
int tot,n,trie[maxn][26],sum[maxn];
bool vis[maxn],flag;
void Insert(char *s,int rt)
{
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        int k = s[i] -a;
        if(trie[rt][k] == 0) trie[rt][k] = ++tot;
        sum[trie[rt][k]]++;
        rt = trie[rt][k];
    }
}
int Find(char *s,int rt)
{
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        int k = s[i] -a;
        rt = trie[rt][k];
        if(rt == 0) return false;
    }
    return true;
}
int main()
{
    tot = 0;
    int rt = 0;
    char s[105];
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> s;
        Insert(s,rt);
    }
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> s;
        if(Find(s,rt)) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

 

CODEVS 4189 (前缀是否出现)

标签:http   ret   pre   dev   include   eof   typedef   前缀   std   

原文地址:https://www.cnblogs.com/LLLAIH/p/11330442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!