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

    题目描写叙述

    遇到单词不认识怎么办? 查字典啊,已知字典中有n个单词,如果单词都是由小写字母组成。

    现有m个不认识的单词,询问这m个单词是否出如今字典中。

    输入

    含有多组測试用例。
    第一行输入n。m (n>=0&&n<=100000&&m>=0&&m<=100000)各自是字典中存在的n个单词和要查询的m个单词.
    紧跟着n行,代表字典中存在的单词。

    然后m行,要查询的m个单词
    n=0&&m=0 程序结束
    数据保证全部的单词都是有小写字母组成,而且长度不超过10

    输出

    若存在则输出Yes,不存在输出No .

    演示样例输入

    3 2
    aab
    aa
    ad
    ac
    ad
    0 0

    演示样例输出

    No
    Yes

    ///字典树是用来查找单词的,是一种哈兮树的变种

    #include<stdio.h>
    #include<string.h>
    struct node
    {
      int next[26];
      int mark;
    }st[500000];
    int top,n,m;
    char a[20],b[20];
    int creat()
    {
      memset(st[top].next,-1,sizeof(st[top].next));
      st[top].mark=0;
      return top++;
    }
    int xiab(char c)
    {
      return c-'a';
    }
    void insert(int root,char *s)
    {
      int i;
      int len=strlen(s);
      for(i=0;i<=len-1;i++)
      {
        if(st[root].next[xiab(s[i])]==-1)
        st[root].next[xiab(s[i])]=creat();
        root=st[root].next[xiab(s[i])];
      }
      st[root].mark=1;
    }
    int search(int root,char *s)
    {
      int i;
      for(i=0;s[i]!='';i++)
      {
        if(st[root].next[xiab(s[i])]==-1)
        return 0;
        root=st[root].next[xiab(s[i])];
      }
      return st[root].mark;
    }
    int main()
    {
      int i,root;
      while(~scanf("%d%d",&n,&m))
      {
        if(n==0 && m==0)
        {
         break;
        }
        else
        {
          top=0;
          root=creat();
          for(i=0;i<=n-1;i++)
          {
            scanf("%s",a);
            insert(root,a);
          }
          for(i=0;i<=m-1;i++)
          {
           scanf("%s",b);
           if(search(root,b))
           printf("Yes
    ");
           else
           printf("No
    ");
          }
        }
      }
    
      return 0;
    }


  • 相关阅读:
    mongoDB在windows下安装和配置.
    node 中的定时器, nextTick()和setImmediate()的使用
    node 通过指令创建一个package.json文件
    node 中 npm报错 Error: ENOENT, stat 'C:UsersAdministratorAppDataRoaming pm'
    canvas之太阳系效果
    canvas之抒写文字
    canvas之绘制一张图片
    canvas之画一个三角形
    canvas之旋转一条线段
    unity3d 2d游戏制作的模式
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6932842.html
Copyright © 2011-2022 走看看