字典的建立 查找

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

 

相关文章:

  • 2018-05-09
  • 2018-01-25
  • 2021-10-07
  • 2021-07-31
  • 2021-10-07
  • 2021-09-02
  • 2021-09-17
  • 2018-08-31
猜你喜欢
  • 2021-10-07
  • 2021-10-07
  • 2021-12-02
  • 2021-11-27
  • 2021-09-29
  • 2017-12-18
  • 2021-11-14
  • 2021-06-12
相关资源
相似解决方案