HDU 4287 Intelligent IME (字典树 map)

分析:此题题意很简单,我就不说了,第一想到的就是用字典树做,首先你得考虑用哪个作为字典树,,显然,这里用后面的字符串作为树更好。

接着就是套模板了。此题WA了无数次,找bug找了一天,也没找到头绪,后来看别人的结题报告,才明白,原来坑爹的把memset(mark)放在了循环外面,一直以为只循环一次就够了,,真是坑爹啊,,,

做到现在了,感觉字典树的问题基本上都能用map来解决。

代码一:字典树

学会了用内联,确实会快一些

#include <iostream>#include <cstring>#include <cstdlib>using namespace std;int cnt;int haash[151];int res[5005];const int MAX = 500000;int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,9};struct trie{trie *next[26];int v; // 数组编号 int vv; // 末尾的标志 void init(){vv = 1;v=-1;memset(next,NULL,sizeof(next));}}heap[MAX];inline trie *new_trie(){heap[cnt].init();return &heap[cnt++];}inline void creat(char *s,trie *p,int num){for(;*s;s++){int id = *s – '0';if(p->next[id] == NULL){p->next[id] = new_trie();}p = p->next[id];}p->v = num;p->vv = -1;}inline void find(char *s,trie *p){for(;*s;s++){p = p->next[a[*s-'a']];if(p == NULL)return ;}if(p->vv == -1){res[p->v]++;}else return ;}int main() {int k,i,m,n,j;char s[10];cnt = 0;trie *root = new_trie();cin>>m;while(m–){cin>>k>>n;getchar();for(i=0;i<k;i++){gets(s);creat(s,root,i);}memset(res,0,sizeof(res));while(n–){gets(s);find(s,root);}for(i=0;i<k;i++){cout<<res[i]<<endl;}} return 0;}代码二:map

#include <iostream>#include <cstring>#include <cstdlib>#include <map>using namespace std;int haash[151];int mark[5001];int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};int main() {int k,i,m,n,j,num,sum;char s[10];map<int,int> mp;scanf("%d",&m);while(m–){scanf("%d%d",&k,&n);for(i=1;i<=k;i++){scanf("%d",&num);mp[num] = i;}memset(mark,0,sizeof(mark));while(n–){scanf("%s",s);for(i=0,sum=0;s[i];i++){sum = sum*10 + a[s[i]-'a'];}mark[mp[sum]]++;}for(i=1;i<=k;i++){printf("%d\n",mark[i]);}} return 0;}代码三:

#include <iostream>#include <cstring>#include <cstdlib>#include <map>using namespace std;int haash[151];int mark[5001];int a[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};int main() {int k,i,m,n,j,num[5001],sum;char s[10];map<int,int> mp;scanf("%d",&m);while(m–){scanf("%d%d",&k,&n);memset(num,0,sizeof(num));for(i=1;i<=k;i++){scanf("%d",&num[i]);}memset(mark,0,sizeof(mark));while(n–){scanf("%s",s);for(i=0,sum=0;s[i];i++){sum = sum*10 + a[s[i]-'a'];}for(i=1;i<=k;i++){if(sum == num[i])mark[i]++;}}for(i=1;i<=k;i++){printf("%d\n",mark[i]);}} return 0;}

Intelligent IMETime Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2721Accepted Submission(s): 1340

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

绊住的不仅是双脚,还有未来。

HDU 4287 Intelligent IME (字典树 map)

相关文章:

你感兴趣的文章:

标签云: