zoukankan      html  css  js  c++  java
  • 字典树-水题

    Hat’s Words

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

     字典树题目: 建字典树,然后将一个单词分两个部分在字典树中查找!!

    用结构体封装一下,感觉还行~~

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>水水更健康<<<<<<<<<<<<<<<<<<<<<<<<<<<

    #include<cstdio>
    #include<cstring>
    #include<cctype>
    #include<cmath>
    #include<set>
    #include<map>
    #include<list>
    #include<queue>
    #include<deque>
    #include<stack>
    #include<string>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<stdlib.h>
    #include<time.h>
    
    using namespace std;
    typedef long long LL;
    const int INF=2e9+1e8;
    const int MOD=1e9+7;
    const int MAXSIZE=2e4+100;
    const double eps=0.0000000001;
    void fre()
    {
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    }
    #define memst(a,b) memset(a,b,sizeof(a))
    #define fr(i,a,n) for(int i=a;i<n;i++)
    
    struct dicTree
    {
        struct Node
        {
            int sum;
            int next[26];
        }Tree[200000];
        int root,sz;
        int newnode()
        {
            for(int i=0;i<26;i++) Tree[sz].next[i]=-1;
            Tree[sz++].sum=0;
            return sz-1;
        }
        void init()
        {
            sz=0;
            root=newnode();
        }
        void insert(string& s,int len)
        {
            int now=root;
            for(int i=0;i<len;i++)
            {
                int to=(int)(s[i]-'a');
                if(Tree[now].next[to]==-1) Tree[now].next[to]=newnode();
                now=Tree[now].next[to];
            }
            Tree[now].sum++;
        }
        bool find(string& s,int start,int len)
        {
            int now=root;
            for(int i=start;i<len;i++)
            {
                int to=(int)(s[i]-'a');
                if(Tree[now].next[to]==-1) return false;
                if(Tree[now].sum&&(!start)&&find(s,i,len)) return true;
                now=Tree[now].next[to];
            }
            if(start&&Tree[now].sum) return true;
            return false;
        }
    };
    dicTree AC;
    string input[50009];
    int main()
    {
        AC.init();
        int tot=0;
        while(cin>>input[tot]) AC.insert(input[tot],(int)input[tot].size()),tot++;
        for(int i=0;i<tot;i++)
        {
            if(AC.find(input[i],0,(int)input[i].size())) cout<<input[i]<<endl;
        }
        return 0;
    }
    
    /**************************************************/
    /**             Copyright Notice                 **/
    /**  writer: wurong                              **/
    /**  school: nyist                               **/
    /**  blog  : http://blog.csdn.net/wr_technology  **/
    /**************************************************/
  • 相关阅读:
    ReactNative--Flexbox布局
    ReactNative--资源,文章,等等
    ReactNative--坑--no bundle URL present
    ReactNative--StyleSheet样式表
    ReactNative--项目创建及结构分析
    ReactNative--ReactNative简介
    10-4路径文字排版 这一节完全不明白
    10-3区域文字排版
    10-2使用字符调板
    10-1使用文字工具
  • 原文地址:https://www.cnblogs.com/coded-ream/p/7207939.html
Copyright © 2011-2022 走看看