HDU 3724 Encoded Barcodes(字典树,计算前缀数)

来源:互联网 发布:黑马程序员 c 课件 编辑:程序博客网 时间:2024/04/28 10:19

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3724


题目:

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 Description
All the big malls need 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 should 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 "A". 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 many 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 should 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.

 

Input
There 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.
 

Output
Output 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
5
Hint
There 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.
 

题目大意:
先给出所有产品名称, 然后输入查询词,  统计以这个查询词为前缀的产品个数。
查询词的输入比较特殊,是输入条形码的每一条的宽度。
宽度共有两种类型,一种比较宽,一种是窄的, 宽的是窄的长度的两倍。 注意, 如果宽的那个是1, 那么窄的是0。
然后把这个条形码转换成二进制数,宽的位数对应1,窄的对应0.
然后会得到一个ASCII码, 是字母'a' ~'z'的范围。


分析与总结:
直接统计前缀,字典树的一个典型应用。 这题的关键是处理条形码。
可以把所有宽度求和, 再求平均长度,大于平均长度的是宽的那条,小于平均长度是窄的那条。



代码:

#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;}



 ——  生命的意义,在于赋予它意义士。

          原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)






原创粉丝点击