HDU 4287 Intelligent IME (Dictionary Tree Array Edition)

Source: Internet
Author: User

Intelligent IME

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4776 Accepted Submission (s): 2227

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 Input13 5466444874GOINNIGHTMIGHTGN

Sample Output320

Title Link: HDU 4287

As a result of the recent study of AC automata, the pointer version of the dictionary tree although good writing but slow and easy to burst memory, so that the film of the array version of the dictionary tree, a little trouble to lose, but the operability is higher than the pointer, the speed is much faster than the pointer ..., and then took this question to test, The array version of the dictionary tree is the use of subscript to replace the role of pointers, and the space is pre-allocated, as long as the subscript is not labeled to a layer, this layer will not be used, is equivalent to not appear, not allocated memory. Tot represents the current number of nodes, obviously at the beginning of the tot=1, because there is a table header (corresponding to the list of NodeList *l), and the table header array subscript is exactly 0, each allocation will l[tot] this layer of content to initialize, and then connect this to L[now]->nxt[v] On. This method is very simple, is to ask you each key sequence can be typed in the following string, it is obvious that the dictionary tree structure with an ID to indicate the current group of keys, and then the next M-character into the corresponding key sequence and then take the query, return the last found the ID and then the corresponding number of keys + 1. The complexity of the space is probably open Max_len * tot_cnt will probably be, specifically unclear how much to open, anyway temporarily open so much seems to be the topic can be. It's really handy to have Init as a member function ....

Code:

#include <stdio.h> #include <bits/stdc++.h>using namespace std; #define INF 0x3f3f3f3f#define CLR (arr,val) memset (arr,val,sizeof (arr)) #define LC (x) (x<<1) #define RC (x) ((x<<1) +1) #define MID (x, y) ((x+y) >>1) typedef pair<int,int> PII;TYPEDEF Long Long ll;const double Pi=acos ( -1.0); const int n=5010;struct trie{int nxt[1    0];    int id;        inline void init () {CLR (nxt,0);    Id=-1; }};    Trie l[n*7];int Tot,ans[n];char S[10];char change[10];void init () {l[0].init ();    Tot=1; CLR (ans,0);}    void Update (char s[],int ID) {int now=0,len=strlen (s);        for (int i=0; i<len; ++i) {int v=s[i]-' 0 '; if (!            L[now].nxt[v]) {l[tot].init ();        l[now].nxt[v]=tot++;    } Now=l[now].nxt[v]; } L[now].id=id;}    int Find (char s[]) {int now=0;    int Len=strlen (s);        for (int i=0; i<len; ++i) {int v=s[i]-' 0 '; if (!        L[NOW].NXT[V]) return-1; NOW=L[NOW].NXT[V]; } return l[now].id;}    inline int getid (const char &x) {if (x>= ' a ' &&x<= ' C ') return 2;    else if (x>= ' d ' &&x<= ' F ') return 3;    else if (x>= ' g ' &&x<= ' I ') return 4;    else if (x>= ' J ' &&x<= ' L ') return 5;    else if (x>= ' m ' &&x<= ' O ') return 6;    else if (x>= ' P ' &&x<= ' s ') return 7;    else if (x>= ' t ' &&x<= ' V ') return 8; else return 9;}    int main (void) {int tcase,n,m,i;    scanf ("%d", &tcase);        while (tcase--) {init ();        scanf ("%d%d", &n,&m);            for (i=0; i<n; ++i) {scanf ("%s", s);        Update (S,I);            } for (i=0; i<m; ++i) {scanf ("%s", s);            int Len=strlen (s);            For_each (s,s+len,[] (char &c) {C=getid (c) + ' 0 ';});            int Indx=find (s);        if (indx!=-1) ++ans[indx]; } for_each (Ans,ans+n, [&] (const int &c) {printf ("%d\n", c);}); } return 0;}

HDU 4287 Intelligent IME (Dictionary tree array version)

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.