uvalive 5026 字典树典型

来源:互联网 发布:mt4软件怎么用 编辑:程序博客网 时间:2024/06/05 14:16

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 pre x 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 rst 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 rst 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
oat 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.
Explanation for the sample:
There is only one test case. The rst 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.
Sample Input
4 3
apple
apple
avatar
book
1
1 2 2 1 1 1 1 2
2
1 2 2 1 1 1 1 2
10.1 20.1 19.9 20.0 10.2 9.8 9.9 10.0
1
1 2 2 1 1 1 2 2
Sample Output
5

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

典型的字典树

#include <bits/stdc++.h>using namespace std;#include <iostream>#include <cstring>#include <cstdio>#include <string>using namespace std;const int sonnum=26,base='a';char s1[12],ss[12];struct Trie{    int num;    bool flag;    struct Trie *son[sonnum];    Trie()    {        num=1,flag=false;        memset(son,0,sizeof(son));    }};Trie *NewTrie(){    Trie *temp=new Trie;    return temp;}void Insert(Trie *root,char *s){    Trie *temp=root;    while(*s){        if(temp->son[*s-base]==NULL)        {            temp->son[*s-base]=NewTrie();        }        else             temp->son[*s-base]->num++;        temp=temp->son[*s-base];        s++;    }    temp->flag=true;}int search(Trie *root,char *s){    Trie *temp=root;    while(*s)    {        if(temp->son[*s-base]==NULL) return 0;        temp=temp->son[*s-base];        s++;    }    return temp->num;}int main(){    while(scanf("%d%d",&n,&m))    {    Trie *root=NewTrie();    root->num=0;    int n, m;    for(int i=0;i<n;i++)    {        scanf(" %s",s1);        Insert(root,s1);    }    int ans=0;    for(int i=0;i<m;i++)    {        int k;        scanf("%d",&k);        double wei[8];        memset(ss,0,sizeof(ss));        for(int kk=0;kk<k;kk++)        {        double sum=0;         for(int j=0;j<8;j++)         {            scanf("%lf",&wei[j]);            sum+=wei[j];         }         int res=0;         sum/=8.0;         for(int j=0;j<8;j++)         {            if(wei[j]>sum)                res=res*2+1;            else res=res*2;         }         ss[kk]=res;        }         ans+=search(root,ss);    }        cout<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击