HDU 4287 Intelligent IME (字典树 && map)
来源:互联网 发布:身份证号脱敏算法 编辑:程序博客网 时间:2024/04/27 18:59
分析:此题题意很简单,我就不说了,第一想到的就是用字典树做,首先你得考虑用哪个作为字典树,显然,这里用后面的字符串作为树更好。
接着就是套模板了。此题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 IME
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2721 Accepted 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, 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?
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.
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
0 0
- HDU 4287 Intelligent IME (字典树 && map)
- Hdu 4287 Intelligent IME 字典树 Hash
- hdu 4287 Intelligent IME (字典树)
- [字典树] HDU 4287 - Intelligent IME
- hdu 4287 Intelligent IME (字典树)
- hdu 4287 Intelligent IME 字典树
- HDU - 4287 Intelligent IME 字典树
- HDU 4287 Intelligent IME(字典树)
- HDU 4287 Intelligent IME(字典树)
- HDU 4287 Intelligent IME (map水题或字典树+DFS)
- HDU-4287 Intelligent IME (Trie 字典树 入门题)
- HDU 4287 Intelligent IME map的应用
- HDU 4287 Intelligent IME(map运用)
- map-hdu 4287-Intelligent IME-想法题
- hdoj 4287 Intelligent IME 字典树
- hdu 4287 Intelligent IME
- HDU-4287-Intelligent IME
- HDU 4287 Intelligent IME
- 关于MVC中Layout母版页文件
- Linux日志清除
- 树莓派玩转WIN10之安装篇
- Integer Game(UVA11489)3的倍数
- 文本过滤
- HDU 4287 Intelligent IME (字典树 && map)
- 微软100题(75)二叉树最低公共父节点
- lab4 Cache Geometries 深入理解计算机系统——高速缓存
- CMSIS-RTOS RTX API创建一个线程的多个运行实例
- 【BZOJ1026】【SCOI2009】windy数 数位DP
- Scala-List
- java 容器类研究一:HashMap源码原理分析
- hadoop fs -put
- [android]APP启动界面——SplashActivity