zoukankan      html  css  js  c++  java
  • 字典的建立 查找

    hdu 1251

    c++

    #include<bits/stdc++.h>
    using namespace std;
    struct node  //  make tree;
    {
        node *next[26];
        int cnt;
        node()
        {
            cnt=0;
            memset(next,0,sizeof(next));
        }
    };
    node root;//  000
    void buildtrie(char *s)
    {
        node *p=&root;
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            if(p->next[s[i]-'a']==NULL)
            {
                p->next[s[i]-'a']=new node;
    
            }
            p=p->next[s[i]-'a'];
            p->cnt++;
        }
    }
    int findtrie(char *s)
    {
        node *p=&root;
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            if(p->next[s[i]-'a']==NULL)
             return 0;
             p=p->next[s[i]-'a'];
        }
        return p->cnt;
    }
    int main()
    {
        char word[11];
        while(cin.getline(word,12))
        {
            if(strlen(word)==0||word[0]==' ')
                break;
            buildtrie(word);
        }
        while(scanf("%s",word)!=EOF)
        {
            printf("%d
    ",findtrie(word));
        }
    }

    #include<iostream>#include<string.h>
    using namespace std;struct node{    node *next[26];    int cnt;    node()    {        cnt=0;        memset(next,0,sizeof(next));    }};node root;void buildtrie(char *s){    node *p=&root;    int l=strlen(s);    for(int i=0;i<l;i++)    {        if(p->next[s[i]-'a']==NULL)        {             p->next[s[i]-'a']=new node;        }        p=p->next[s[i]-'a'];        p->cnt++;    }}int findtrie(char *s){    node *p=&root;    int l=strlen(s);    for(int i=0;i<l;i++)    {        if(p->next[s[i]-'a']==NULL)        return 0;        p=p->next[s[i]-'a'];    }    return p->cnt;
    }int main(){    char s[12];    while(cin.getline(s,12))    {        if(strlen(s)==0||s[0]==' ')            break;        buildtrie(s);    }    //cout<<"-----"<<endl;    while(scanf("%s",s)!=EOF)    {        printf("%d ",findtrie(s));    }}

  • 相关阅读:
    Ubuntu 16.04安装迅雷(兼容性不高)
    Ubuntu 16.04安装QQ(不一定成功)
    Ubuntu查看隐藏文件夹的方法
    Ubuntu下非常规方法安装绿色软件(压缩包)
    Ubuntu下常规方法安装软件
    Ubuntu 16.04下截图工具Shutter
    java中 awt Graphics2D
    Vue2.0总结———vue使用过程常见的一些问题
    MySQL 中隔离级别 RC 与 RR 的区别
    DBAplus社群线上分享----Sharding-Sphere之Proxy初探
  • 原文地址:https://www.cnblogs.com/Andromeda-Galaxy/p/9443894.html
Copyright © 2011-2022 走看看