GZS与古英文字典(字典树)

有一天, GZS得到一本古代的英文字典, 很可惜, 由于年代久远, 很多单词都看不清楚了. 但是, 这怎么能难倒我们的G神呢, G神想要用这本字典查询一些单词, 下面给出字典中的内容. 字典中的单词全部由小写字母组成, 字迹模糊的部分用'?'或者'*'来表示, '?'可以匹配一个小写字母, '*'可以匹配0个或者1个或者多个小写字母. 例如, a?b可以代表aab, abb......azb, 而a*b可以代表ab, aab, abb...azb, aaab, aabb......然后给出一部分待查找的单词, 全部由小写字母组成. 如果能在字典中找到这个单词, 请输出1, 否则, 输出0.对于这种小事GZS觉得很是无聊, 你快来写个程序帮帮他吧.

第一行包含一个整数T(T <= 1000), 表示测试数据的组数.
每组测试数据的第一行有两个整数n(0 < n < 1000) 和m (0 < m < 1000), 
分别表示字典中单词的数目和待查找的单词的数目
接下来n行, 代表字典中的单词, 每个单词由小写字母和'?'以及'*'组成, 单词长度不超过6.
接下来m行, 代表待查找的单词, 每个单词由小写字母组成, 单词长度不超过20.

每组数据输出两行
第一行 Case #x:, x表示组数编号,从1开始.
第二行 对于每个待查找的单词, 如果存在于字典中, 则输出1, 否则, 输出0.

  复制
2
2 3
*
?
abj
ab
a
3 4
ab?d
a*e
abc
abcd
abcde
ax
ae
Case #1:
111
Case #2:

1101

建立字典书,把可能查到的单词存到字典树上,后面直接查询。

#include <map>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef struct tree{
        tree *next[30];
        int over;
}tree;

tree * createTreeNode(){
           tree * p  = (tree *)malloc(sizeof(tree));
           for(int i = 0; i < 30; i++)   p->next[i] = NULL;
           p->over  = 0;
           return p;
}
bool isOver(char str[])
{
        for(int i = 0;str[i]; i++)
             if(str[i] != '*') return 0;
        return 1;
}
void renewTree(tree * pcur,char str[])
{
         if(str[0] == '\0')
         {
                pcur->over = 1;
                return;
         }
         int x;
         if(str[0] == '?')  x = 26;
         else if(str[0] == '*')  x = 27;
         else    x = str[0] -  'a';
         if(pcur->next[x] == NULL)
                pcur->next[x] = createTreeNode();
         pcur->over = isOver(str) || pcur->over;  // 在原来的基础上更新是否结束。
         renewTree(pcur->next[x],str+1);
}
bool yes;
void enquryTree(tree * pcur, char str[])
{
         if(yes) return;
         if(str[0] == '\0')
         {
                yes = pcur->over;
                return;
         }
         int x = str[0] - 'a';
         if(pcur->next[26])
               enquryTree(pcur->next[26] , str+1);
         if(pcur->next[x])
               enquryTree(pcur->next[x] , str+1) ;
         if(pcur->next[27])
         {
               int len = strlen(str);
               for(int i = 0; i <= len; i++)
                    enquryTree(pcur->next[27],str+i);
         }
}
int ans [1010];
int main()
{
    int T,n,m;
    char str[30];
    scanf("%d",&T);
    int c = 1;
    while(T-- )
    {
          tree * root = createTreeNode();
          scanf("%d%d",&n,&m);
          for(int i = 0; i < n; i++)
          {
               scanf("%s",str);
               renewTree(root,str);
          }
          int k = 0;
          for(int i = 0; i < m;i++)
          {
              yes = 0;
              scanf("%s",str);
              enquryTree(root,str);
              ans[k++] = yes;
          }
          printf("Case #%d:\n",c++);
          for(int i = 0; i < k; i++)
              printf("%d",ans[i]);
          printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值