HDU-Intelligent IME (Dictionary tree)

Source: Internet
Author: User

HDU-Intelligent IME (Dictionary tree)
Intelligent IMETime 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

13 5466444874goinnightmightgn

Sample Output
320

Source 2012 ACM/ICPC Asia Regional Tianjin Online
Question: 2 ~ 9 each digit represents its own letter, and the following English words can be produced for each digit string.
Analysis: At first, I built a dictionary tree for the given words, and then the dfs numeric string, enumerating the number of words that a numeric string can constitute, so there is no doubt that it is. Later, I changed the method to create a dictionary tree that provides a number string, and then
Hash is the corresponding numeric string, and then query.
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4287
AC code:
# Include
 
  
# Include
  
   
# Include
   
    
# Includeusing 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
    
     
Next [pos] = NULL) {q = new trie; q-> first = 0; for (int j = 0; j
     
      
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
      
        Next [pos] = NULL) return 0; p = p-> next [pos];} return p-> first;} void delTrie (trie * Root) {for (int I = 0; I
       
         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
        
          Next [I] = NULL; memset (ans, 0, sizeof (ans); memset (str, '\ 0', sizeof (str )); scanf ("% d", & N, & M); for (int I = 1; I <= N; I ++) {scanf ("% s ", str); createTrie (str, I);} for (int I = 0; I
         
           TLE Code: (trie + dfs)
          
#include
           
            #include
            
             #include
             
              #includeusing 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
              
               next[pos]==NULL){            q=new trie;            q->point=false;            for(int j=0;j
               
                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
                
                 next[pos]==NULL) return false; p=p->next[pos]; } return p->point;}void delTrie(trie *Root){ for(int i=0;i
                 
                  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;inext[i]=NULL; scanf("%d%d",&N,&M); for(int i=0;i
                  
                 
                
               
              
             
            
           

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.