Analysis: This problem test instructions very simple, I will not say, the first thought is to use a dictionary tree, first you have to consider which as a dictionary tree, obviously, here with the following string as a tree better.
Then there is the set of templates. This problem wa no several times, find a bug to find a day, also did not find the clue, and then see other people's Knot report, just understand, the original pit dad put memset (Mark) in the loop outside, always thought that only a loop once is enough,, really pit daddy Ah,,,
Do now, feel the problem of the dictionary tree basically can be solved with map.
Code One: Dictionary tree
Learned to use inline, and it's really going to be faster
#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; Array number int VV; The flag at the end of 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;}
Code two: 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;}
Code Three:
#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 is familiar with the intelligent 中文版 Input method on the cell phone. To is specific, the number buttons may correspond to some 中文版 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, and then the input method would choose from an embedded Dictionary, all words matching the input number sequence, such as "Wing", "Whoi", "Zhog". Here comes we 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 the which is formatted like this:
Both integer n (1 <= n <=), M (1 <= m <=), indicating the number of input number sequences and the Nu Mber 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 this there is 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 Numbe R sequence, each integer per line.
Sample Input
5466444874goinnightmightgn
Sample Output
320
Source2012 ACM/ICPC Asia Regional Tianjin Online
HDU 4287 Intelligent IME (dictionary tree && map)