• HDU 1251 统计难题


    题意还是比較简单的,思路也比較好想!

    做本题首先要理解什么是tire树,我的一篇转载的文章有非常具体的过程:http://blog.csdn.net/u012313382/article/details/38493113

    看完了那篇文章就直接给AC代码吧(自己參照模板打的代码):

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<stdlib.h>
    using namespace std;
    #define maxn 26
    typedef struct  node    //num是经过的次数
    {
        //int str;          //str在本题中没实用处。可是在统计单词的问题上还是能够去记录单词数的
        int num;
        struct node *next[maxn];
    }trienode,*trie;
    struct node *root;
    void Insert(char *s)
    {
        int i,len=strlen(s);
        trienode *p=root,*q;
        for(i=0;i<len;i++)
        {
            if(p->next[s[i]-'a']==NULL) //假设这个它的子节点没有。则新建一个
            {
                q=(struct node  *)malloc(sizeof(struct node));
                memset(q->next,NULL,sizeof(q->next));
                q->num=1;
              //  q->str=0;
                p->next[s[i]-'a']=q;
            }
            else
            {
                p->next[s[i]-'a']->num++;
            }
            p=p->next[s[i]-'a'];
        }
       // p->str=1;
    }
    int find(char *s)
    {
        int i,len=strlen(s);
        trienode *p=root,*q;
        for(i=0;i<len;i++)
        {
            if(p->next[s[i]-'a']==NULL)
                return 0;
            p=p->next[s[i]-'a'];
        }
        return p->num;
    }
    int main()
    {
        char s[maxn];
        root=(struct node *)malloc(sizeof(struct node));
        memset(root->next,NULL,sizeof(root->next));
       // root->str=0;
        root->num=0;
        while(gets(s))
        {
            if(s[0]==0)
                break;
            Insert(s);
        }
        while(cin>>s)
            cout<<find(s)<<endl;
        return 0;
    }
    

    自己理解后的AC代码

    #include<iostream>
    # include<cstring>
    #include<cstdio>
    using namespace std;
    const int maxn=500000;
    char wen[20];
    struct node{
        int tot;
        int child[26];
        node()
        {
            tot=0;
            memset(child,0,sizeof(child));
        }
    }tree[maxn];
    int sz=0;
    void insert(char *s)
    {
    
        int u=0;
        int h=strlen(s);
        for(int i=0;i<h;i++)
        {
            int pos=s[i]-'a';
            if(tree[u].child[pos]==0)
                tree[u].child[pos]=++sz;
            u=tree[u].child[pos];
            tree[u].tot++;
        }
    }
    int find(char *t)
    {
    
       int u=0,h=strlen(t);
       for(int i=0;i<h;i++)
       {
           int pos=t[i]-'a';
           if(tree[u].child[pos]==0)
            return 0;
           u=tree[u].child[pos];
       }
       return tree[u].tot;
    }
    int main()
    {
        while(gets(wen))
        {
            if(strlen(wen)==0)  break;
            insert(wen);
        }
        while(gets(wen))
        {
              printf("%d
    ",find(wen));
        }
        return 0;
    }
    


  • 相关阅读:
    JavaWeb
    List的三种遍历(转)
    Java中父类和子类集合互相转换(转)
    Day4
    Ubuntu20.04配置 ES7.17.0集群
    K8S中部署apisix(非ingress)
    使用 Flutter&&Hive&&Bloc 写一个待办小demo
    SQL多行数据分组后合并某个字段
    sql group 拼接字段
    Socket
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5349821.html
Copyright © 2020-2023  润新知