zoukankan      html  css  js  c++  java
  • trie字典树

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

    题目链接: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;
    }

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

  • 相关阅读:
    work_7_Boolean和boolean,基本类型和包装类型
    work_06_服务器上传图片错误
    work_05_64未随机id生成器
    work_04_谷歌验证码工具Kaptcha
    vue.js_13_vue的搭建
    每日一java(割草机)
    work_03_常见jq问题处理
    work_02_css样式
    java 27
    java 27
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/10661732.html
Copyright © 2011-2022 走看看