于是他错误的点名开始了(字典树模板)

QAQ
字典树第一题,可以说是板子题了。
在结构体中再加入一个表示有没有叫过的变量就行了2333

#include <cstdio>  
#include <string>  
#include <cstdlib>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
using namespace std;  
#define idx(x) x - 'a';
struct Trie{
    int net[26];//26个孩子
    int val;//以这个节点为结束的单词的个数
    int f;//有没有叫这个人
}tree[999999];
int nxt,n;
char ss[200];
int add()
{
    memset(&tree[nxt],0,sizeof(Trie));
    return nxt++;
}
void cr(char *s)
{
    int rt=0,len=strlen(s);

    for(int i=0;i<len;i++)
    {
        int c=idx(s[i]);
        if(!tree[rt].net[c])
        {
            tree[rt].net[c]=add();
        }
        rt=tree[rt].net[c];
    }
    tree[rt].val++;
}
int find(char *s)
{

    int rt=0,len=strlen(s);

    for(int i=0;i<len;i++)
    {
        int c=idx(s[i]);
        if(!tree[rt].net[c])
         return 0;
        rt=tree[rt].net[c];
    }
    if(!tree[rt].val) return 0;//没有返回0
    else if(tree[rt].f)
    {
        return 2;//重复返回2
    }
    tree[rt].f=1;
    return 1;//第一次叫有这个人返回1
}
int main()
{
    memset(&tree[0],0,sizeof(Trie));
    nxt=1;


    scanf("%d",&n);

    while(n--)
    {
        scanf("%s",ss);
        cr(ss);
    } 

    scanf("%d",&n);

    while(n--)
    {
        scanf("%s",ss);
        int a=find(ss);
        if(a==0) printf("WRONG");
        if(a==1) printf("OK");
        if(a==2) printf("REPEAT");
        puts(""); 
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值