HDU_4287_Intelligent IME(字典树)

Intelligent IME

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2681    Accepted Submission(s): 1322



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
 

Source
 
题意:手机英文输入法九宫格键盘中,2~9数字各自代表了自己的字母,求给出的几个数字串分别能够打出下列几个英文单词。
分析:一开始我是建立给出的单词的字典树,然后dfs数字串,枚举数字串能够组成的单词数,这样毫无疑问地TLE了。后来换了种方法,建立给出数字串的字典树,然后把单
            词串hash为对应的的数字串,再进行查询。
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4287
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 10 ;
const int maxn = 5005 ;
struct trie{
    int first;
    trie *next[MAX];
};

trie *root;

int T,N,M;
char str[7],ss;
int ans[maxn],slen;
void createTrie(char *s,int n){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;i<len;i++){
        pos=s[i]-'0';
        if(p->next[pos]==NULL){
            q=new trie();
            q->first=0;
            for(int j=0;j<MAX;j++)
                q->next[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->first=n;
}

int findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;i<len;i++){
        pos=s[i]-'0';
        if(p->next[pos]==NULL)
            return 0;
        p=p->next[pos];
    }
    return p->first;
}

void delTrie(trie *Root){
    for(int i=0;i<MAX;i++){
        if(Root->next[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

char getLetter(char s){
    if(s>='a'&&s<='c') return '2';
    else if(s>='d'&&s<='f') return '3';
    else if(s>='g'&&s<='i') return '4';
    else if(s>='j'&&s<='l') return '5';
    else if(s>='m'&&s<='o') return '6';
    else if(s>='p'&&s<='s') return '7';
    else if(s>='t'&&s<='v') return '8';
    else return '9';
}

int main(){
    scanf("%d",&T);
    while(T--){
        root=new trie();
        for(int i=0;i<MAX;i++)
            root->next[i]=NULL;
        memset(ans,0,sizeof(ans));
        memset(str,'\0',sizeof(str));
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++){
            scanf("%s",str);
            createTrie(str,i);
        }
        for(int i=0;i<M;i++){
            scanf("%s",str);
            slen=strlen(str);
            for(int j=0;j<slen;j++){
                ss=str[j];
                str[j]=getLetter(ss);
            }
            int an=findTrie(str);
            if(an) ans[an]++;
        }
        for(int i=1;i<=N;i++)
            printf("%d\n",ans[i]);
        delTrie(root);
    }
    return 0;
}

TLE代码:(trie+dfs)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 28;
const int maxn = 5005;
char str[10][4]={{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},
                {'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
int ank[8]={3,3,3,3,3,4,3,4};

struct trie{
    bool point;
    trie *next[MAX];
};

struct edge{
    int n;
    int x[6];
}num[maxn];

trie *root;
char s[7];
int T,N,M,slen,nn,sum;
int vis[10][7];
void createTrie(char *s){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;i<len;i++){
        pos=s[i]-'a';
        if(p->next[pos]==NULL){
            q=new trie();
            q->point=false;
            for(int j=0;j<MAX;j++)
                q->next[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->point=true;
}

bool findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;i<len;i++){
        pos=s[i]-'a';
        if(p->next[pos]==NULL)
            return false;
        p=p->next[pos];
    }
    return p->point;
}

void delTrie(trie *Root){
    for(int i=0;i<MAX;i++){
        if(Root->next[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

void findAns(int a[],int k,int n,char *s){
    if(k==n){
        //printf("%s  ",s);
        if(findTrie(s)){
            sum++;
        }return ;
    }
    int kk=a[k]-2;
    for(int i=0;i<ank[kk];i++){
        if(!vis[k][i]){
            s[k]=str[kk][i];
            vis[k][i]=1;k++;
            findAns(a,k,n,s);
            k--;vis[k][i]=0;
        }
    }return ;
}

int main(){
    scanf("%d",&T);
    while(T--){
        nn=0;
        root=new trie();
        for(int i=0;i<MAX;i++)
            root->next[i]=NULL;
        scanf("%d%d",&N,&M);
        for(int i=0;i<N;i++){
            scanf("%s",s);
            slen=strlen(s);
            for(int j=0;j<slen;j++)
                num[nn].x[j]=s[j]-'0';
            num[nn].n=slen;
            nn++;
        }

        for(int i=0;i<M;i++){
            scanf("%s",s);
            createTrie(s);
        }

        for(int i=0;i<nn;i++){
            sum=0;
            memset(s,'\0',sizeof(s));
            memset(vis,0,sizeof(vis));
            //for(int j=0;j<num[i].n;j++) cout<<num[i].x[j]<<" ";
            findAns(num[i].x,0,num[i].n,s);
            printf("%d\n",sum);
        }
        delTrie(root);
    }return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值