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

    附上一篇博客http://blog.csdn.net/cambridgeacm/article/details/7752247

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    struct node
    {
        int coun;
        node* child[26];
        node()
        {
            coun=0;
            for(int i=0;i<26;i++)
                child[i]=NULL;
        }
    };
    
    node* root=new node;
    node* current;
    node* newnode;
    
    void inser(char* str)
    {
        current=root;
        for(int i=0;i<strlen(str);i++)
        {
            int m=str[i]-'a';
            if(current->child[m]!=NULL)
            {
                current=current->child[m];
                (current->coun)++;
            }
            else
            {
                newnode=new node;
                current->child[m]=newnode;
                current=newnode;
                (current->coun)++;
            }
        }
    }
    
    int searc(char* str)
    {
        current=root;
        for(int i=0;i<strlen(str);i++)
        {
            int m=str[i]-'a';
            if(current->child[m]!=NULL)
            {
                current=current->child[m];
            }
            else
                return 0;
        }
        return current->coun;
    }
    
    int main()
    {
        char str[20];
        while(gets(str),strcmp(str,""))
            inser(str);
        while(scanf("%s",str)!=EOF)
            cout<<searc(str)<<endl;
        return 0;
    }
  • 相关阅读:
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    问题清单
    2020软件工程个人作业06
    2020软件工程作业05
    2020软件工程作业00
    2020软件工程作业04
    2020软件工程作业03
  • 原文地址:https://www.cnblogs.com/wsruning/p/5525772.html
Copyright © 2011-2022 走看看