HDU 4287 Intelligent IME(字典树)

题意:给你几串数字,每一串数字都是拼音九键的按键顺序。再给你几个单词,问你从刚才的按键顺序中分别能按出多少个给定的单词。

思路:用map直接将每个字母映射为拼音九键对应的数字,再插入到字典树,插入完后对应位置u的v[u]+1,v[u]表示打到u这个位置能打出的给定的单词的单词数。总体来说还是比较简单的。

AC代码:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
using namespace std;
const int mx=1000005;
const int inf=0x3f3f3f3f;
int num,cas,n,m,T;
map<char,int> mp;
struct node{
    int ch[mx][10];
    int v[mx];
    int sz;
    void add(char *s)
    {
     int u=0,n=strlen(s);
     for(int i=0;i<n;i++)
     {
      int id=mp[s[i]];
      if(ch[u][id]==0)
      {
       ch[u][id]=sz;
       v[sz]=0;
       memset(ch[sz],0,sizeof(ch[sz]));
       sz++;
      }
      u=ch[u][id];
     }
     v[u]++;
    }
    void find(char *s)
    {
     int u=0,n=strlen(s);
     for(int i=0;i<n;i++)
     {
      int id=s[i]-'0';
      if(ch[u][id]==0) {printf("0\n");return;}
      u=ch[u][id];
     }
     printf("%d\n",v[u]);
     }
     void init(){
     memset(ch[0],0,sizeof(ch[0]));
     v[0]=0;
     sz=1;
     mp['a']=2;mp['b']=2;mp['c']=2;mp['d']=3;mp['e']=3;mp['f']=3;
     mp['g']=4;mp['h']=4;mp['i']=4;mp['j']=5;mp['k']=5;mp['l']=5;
     mp['m']=6;mp['n']=6;mp['o']=6;mp['p']=7;mp['q']=7;mp['r']=7;mp['s']=7;
     mp['t']=8;mp['u']=8;mp['v']=8;mp['w']=9;mp['x']=9;mp['y']=9;mp['z']=9;
     }
}a;
int main()
{
    //ios::sync_with_stdio(false);
    cas=0;
    char c[5005][10];
    char s[65];
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            a.init();
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++) scanf("%s",c[i]);
            for(int i=1;i<=m;i++) {
             scanf("%s",s);
             a.add(s);
            }
            for(int i=1;i<=n;i++)
            a.find(c[i]);
        }
    }
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值