Intelligent IME(字典树)

Intelligent IME

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2681Accepted 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, c3 : d, e, f4 : g, h, i5 : j, k, l6 : m, n, o  7 : p, q, r, s8 : t, u, v9 : 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

13 5466444874goinnightmightgn

Sample Output

320

Source

2012 ACM/ICPC Asia Regional Tianjin Online

题意:手机英文输入法九宫格键盘中,2~9数字各自代表了自己的字母,求给出的几个数字串分别能够打出下列几个英文单词。分析:一开始我是建立给出的单词的字典树,然后dfs数字串,枚举数字串能够组成的单词数,这样毫无疑问地TLE了。后来换了种方法,建立给出数字串的字典树,,然后把单 词串hash为对应的的数字串,再进行查询。题目链接:?pid=4287AC代码:#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;}

没有口水与汗水,就没有成功的泪水。

Intelligent IME(字典树)

相关文章:

你感兴趣的文章:

标签云: