首页 > 其他 > 详细

trie字典树

时间:2019-04-06 15:10:15      阅读:117      评论:0      收藏:0      [点我收藏+]

---恢复内容开始---

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
const int N=1e6+10;
const int mod=19930726;
void read(int &a)
{
    a=0;
    int d=1;
    char ch;
    while(ch=getchar(),ch>9||ch<0)
        if(ch==-)
            d=-1;
    a=ch-0;
    while(ch=getchar(),ch>=0&&ch<=9)
        a=a*10+ch-0;
    a*=d;
}
void write(int x)
{
    if(x<0)
        putchar(45),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+0);
}
int trie[400001][30],len,root,tot,sum[400001];
bool p;
char s[15];
void ins()
{
    len=strlen(s);
    root=0;
    for(re int i=0;i<len;i++)
    {
        int id=s[i]-97;
        if(!trie[root][id])
            trie[root][id]=++tot;
        root=trie[root][id];
        sum[root]++;
    }
}
int sear()
{
    root=0;
    len=strlen(s);
    for(re int i=0;i<len;i++)
    {
        int id=s[i]-97;
        if(!trie[root][id])
            return 0;
        root=trie[root][id];
    }
    return sum[root];
}
int main()
{
    int f=0;
    while(gets(s))
    {
        if(strlen(s)==0)
        {
            f=1;
            continue;
        }
        if(!f)
            ins();
        else
            cout<<sear()<<endl;
    }
    return 0;
}

 

---恢复内容结束---

trie字典树

原文:https://www.cnblogs.com/acm1ruoji/p/10661732.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!