tjut 1251

来源:互联网 发布:电子书阅读器 知乎 编辑:程序博客网 时间:2024/06/03 05:52
#include<iostream>  #include<cstdio>  #include<cstring>  #include<cstdlib>  using namespace std;  struct node  {      int count;      node *childs[26];      node()      {          count=0;          int i;          for(i=0;i<26;i++)          childs[i]=NULL;      }  };  node *root=new node;  node *current,*newnode;  void insert(char *str)  {      int i,m;      current=root;      for(i=0;i<strlen(str);i++)      {          m=str[i]-'a';          if(current->childs[m]!=NULL)          {              current=current->childs[m];              ++(current->count);          }          else          {              newnode=new node;              ++(newnode->count);              current->childs[m]=newnode;              current=newnode;          }      }  }  int search(char *str)  {      int i,m;      current=root;      for(i=0;i<strlen(str);i++)      {          m=str[i]-'a';          if(current->childs[m]==NULL)          return 0;          current=current->childs[m];      }      return current->count;  }  int main()  {      char str[20];      while(gets(str),strcmp(str,""))      insert(str);      while(gets(str)!=NULL)      printf("%d\n",search(str));      return 0;  }  

0 0