HDU 3724 encoded barcodes (Dictionary tree, number of prefixes calculated)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3724

Question:

Encoded barcodes

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1022 accepted submission (s): 337


Problem descriptionall the big mallneed a powerful system for the products retrieval. Now you are employed design a sub-system: Reading the barcodes and return the matching products.

A barcode is an optical machine-readable representation of data, which shows certain data on certain products. A barcode consists of a series of bars with different widths. in our system, the barcodes have been scanned and The widths have been recorded. every
Consecutive eight bars are considered as representing the ASCII code of a character, each bar for each bit. ideally, there shoshould be only two kinds of widths in the eight bars, and the width of the wider bar is twice of the narrower. the wider bar indicates
1, while the narrower indicates 0. however, due to the inaccuracy of printing and scanning, there will be an error of at most 5%. that is, if the pretended exact width is X, you may get a value in the range [0.95x, 1.05x].

For example, the width sequence "10.0 20.0 10.0 10.0 10.0 10.0 10.0 20.0" is a valid barcode of our system, and it means (01000001) 2, which is (65) 10 and the corresponding character is "". note that "10.5 20.1 10.1 10.2 9.9 9.7 10.0 19.9"
Is also a valid barcode representing the same letter.

You are given the names of all the products and your queries. every name contains lower-case letters only, and the length is no more than 30. the queries are represented as barcodes. for each query, you shoshould decode it to a string S, and report the amount
Of products whose prefix is S. For the output may be very large, you only need to output the sum of all the queries for each case.

 


Inputthere are several test cases in the input. the first line of each case contains two integers n and M (1 <= n <= 10000, 1 <= m <= 2000), indicating the number of products and queries. then n lines follow, indicating the names of the products. note that the names
May be duplicated. then M query blocks follow. the first line of each query block is an integer K (0 <k <= 30) indicating the length of the query, then K lines follow, each line contains 8 positive float numbers, indicating the barcode for each character.

You can assume that the barcodes are always valid, and always represent lower-case letters.


Outputoutput one line for each test case, indicating the sum of all the query results as described above.

 


Sample Input

4 3appleappleavatarbook11 2 2 1 1 1 1 221 2 2 1 1 1 1 210.1 20.1 19.9 20.0 10.2 9.8 9.9 10.011 2 2 1 1 1 2 2
 


Sample output

5HintThere is only one test case. The first query is "a", and the answer is 3. The second query is "ap", and the answer is 2. The third query is "c", and the answer is 0. So the total sum is 3+2+0 = 5. 
 
Question:

First, give the names of all products, then enter the query term, and count the number of products prefixed with the query term. The query word input is special. It is the width of each input bar code. There are two types of width: one is relatively wide, the other is narrow, and the width is twice the narrow length. Note that if the width is 1, the width is 0. Then convert the barcode to the binary number. The number of digits in width corresponds to 1, and the number of digits in width corresponds to 0. Then an ascii code is obtained, which is the letter 'A '~ The range of 'Z.


Analysis and Summary:Directly count the prefix, a typical application of the dictionary tree. The key to this question is to handle the bar code. We can sum all the widths and calculate the average length. The width is greater than the average length, and the width is smaller than the average length.


Code:

#include<iostream>#include<cstdio> #include<cstring>using namespace std;const int KIND = 26;const int MAXN = 500000;int cnt_node;double width[8];struct node{    int cnt;    node* next[KIND];    void init(){        cnt = 0;        memset(next, 0, sizeof(next));    }}Heap[MAXN];inline node* new_node(){    Heap[cnt_node].init();    return &Heap[cnt_node++];}void insert(node* root, char *str){    for(char *p=str; *p; ++p){        int ch=*p-'a';        if(root->next[ch]==NULL)            root->next[ch] = new_node();        root = root->next[ch];        ++root->cnt;    }}int count(node* root, char *str){    for(char *p=str; *p; ++p){        int ch=*p-'a';        if(root->next[ch]==NULL)             return 0;        root=root->next[ch];    }    return root->cnt;}int main(){    int n,m,q;    char str[40];    while(~scanf("%d%d",&n,&m)){        // Trie tree init        cnt_node=0;        node *root = new_node();        // word input        for(int i=0; i<n; ++i){            scanf("%s",str);            insert(root, str);        }        int ans=0;        while(m--){            scanf("%d",&q);            memset(str, 0, sizeof(str));            int p=0;            for(int k=0; k<q; ++k){                double x;                double sum=0;                for(int i=0; i<8; ++i){                    scanf("%lf",&width[i]);                    sum += width[i];                }                sum /= 8.0;                int ch=0;                for(int i=7, d=1; i>=0; --i){                    if(width[i] > sum){                        ch |= d;                    }                    d <<= 1;                }                if(ch>='a' && ch<='z')                    str[p++] = (char)ch;            }            ans += count(root, str);        }        printf("%d\n",ans);    }    return 0;}

 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)

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.