hdu: 4287

Problem Description
  We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below:
  2 : a, b, c 3 : d, e, f 4 : g, h, i 5 : j, k, l 6 : m, n, o
  7 : p, q, r, s 8 : t, u, v 9 : w, x, y, z
  When we want to input the word “wing”, we press the button 9, 4, 6, 4, then the input method will choose from an embedded dictionary, all words matching the input number sequence, such as “wing”, “whoi”, “zhog”. Here comes our question, given a dictionary, how many words in it match some input number sequences?


Input
  First is an integer T, indicating the number of test cases. Then T block follows, each of which is formatted like this:
  Two integer N (1 <= N <= 5000), M (1 <= M <= 5000), indicating the number of input number sequences and the number of words in the dictionary, respectively. Then comes N lines, each line contains a number sequence, consisting of no more than 6 digits. Then comes M lines, each line contains a letter string, consisting of no more than 6 lower letters. It is guaranteed that there are neither duplicated number sequences nor duplicated words.


Output
  For each input block, output N integers, indicating how many words in the dictionary match the corresponding number sequence, each integer per line.


Sample Input
1
3 5
46
64448
74
go
in
night
might
gn


Sample Output
3
2
0
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
const int maxn=50009;
char str[maxn][10],ste[10];
map<string,int> mm;
char ss[28]="22233344455566677778889999";
struct trie
{
    int next[32];
    int cnt;
    bool vis;
    void init()
    {
        vis=false;
        cnt=0;
        memset(next,0,sizeof(next));
    }
}tree[maxn];
int index;
void insert(char *s)
{
    int len=strlen(s);
    int p=0;
    for(int i=0;i<len;i++)
    {
        int x=s[i]-'0';
        if(tree[p].next[x]==0)
        {
            tree[index+1].init();
            tree[p].next[x]=++index;

        }

        p=tree[p].next[x];
        tree[p].cnt++;
    }
    tree[p].vis=true;
}
void find(char *s)
{
    int len=strlen(s);
    int p=0;
    int flag=0;
    int x;
    for(int i=0;i<len;i++)
    {
        x=s[i]-'0';
        if(tree[p].next[x]==0)
         {
             flag=1;
             printf("0\n");
             return;
         }
         p=tree[p].next[x];
    }
    if(tree[p].vis&&!flag)
    printf("%d\n",tree[p].cnt);
    else
    printf("0\n");


}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {    //mm.clear();
        index=0;
        tree[0].init();
        //char sss[maxn][12];
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s",str[i]);

        for(int i=0; i<m; i++)
        {
            scanf("%s",ste);
            int len=strlen(ste);
            char sss[12];
            for(int j=0; j<len; j++)
            {
                sss[j]=ss[ste[j]-'a'];
            }
            sss[len]='\0';
            //mm[sss]++;
            //in++;
            //printf("%s\n",sss);
            insert(sss);
        }
//        for(int i=0; i<m; i++)
//        {
//            //string s=(string)(sss[i]);
//            mm[sss[i]]++;
//            //printf("%s\n",sss[i]);
//            //insert(sss[i]);
//        }
        for(int i=0; i<n; i++)
        {
            //printf("%d\n",mm[str[i]]);
            find(str[i]);
        }



    }
}

發佈了65 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章