首页 > 其他 > 详细

字典的建立 查找

时间:2018-08-08 17:14:58      阅读:163      评论:0      收藏:0      [点我收藏+]

技术分享图片

hdu 1251

#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\n",findtrie(word));
    }
}

 

字典的建立 查找

原文:https://www.cnblogs.com/Andromeda-Galaxy/p/9443894.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!