UVALive 5029 字典树

E - Encoded Barcodes

Description

 
Download as PDF
 

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$ le$N$ le$10000, 1$ le$M$ le$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$ le$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.

Explanation for the sample:

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.

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



题意:      给你一堆商品的名字,然后给你一些条形码,问你这些条形码转换成的字符串的 前缀在商品中出现的个数,条形码的每个字母是八个二进制数字,有两种数,大的是小的2倍,小的是0,大的是1,这里面的吴超是 *0.95---*1.05之间。
思路:       显然是字典树,字典树处理前缀出现次数,先把所有字符串加到树里面,然后我

们想办法吧这个二进制数字翻译成字母,其实很简单,先找到一个最大的,然后枚举每一个 int(max * 1.05 / (num[i] * 0.95)) ,如果他是1,那么当前这位是1,否则当前这位是0 ,然后转换成十进制。然后直接在树上查找就行了。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
using namespace std;
struct trie{
    trie* next[26];
    int num;
    trie(){
        for(int i=0; i<26; i++)
            next[i]=NULL;
        num=1;
    }
} *root=new trie;
void insert(char* s)
{
    trie *p=root;
    for(int i=0; s[i]!=''; i++)
    {
        if(p->next[s[i]-'a']==NULL){
            p->next[s[i]-'a']=new trie;
            p=p->next[s[i]-'a'];
        }
        else{
            p=p->next[s[i]-'a'];
            p->num++;
        }
    }
}
int find(char *s){
    trie *p=root;
    for(int i=0; s[i]!=''; i++)
        if(p->next[s[i]-'a']==NULL)
            return 0;
        else
            p=p->next[s[i]-'a'];
    return p->num;
}
char str[50];
double a[10];
int b[10];
char str1[100000];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
           // getchar();
          // for(int i=0;i<26;i++)
             //   root->next[i]=NULL;
          // root->num=1;
            for(int i=1;i<=n;i++){
                   // memset(str,0,sizeof(str));
                scanf("%s",str);
                insert(str);
            }
            int ans=0;
         for(int i=1;i<=m;i++){
            int k;
            scanf("%d",&k);
            for(int j=0;j<k;j++){
                    double sum=0;
                for(int ii=0;ii<8;ii++){
                    scanf("%lf",&a[ii]);
                     sum+=a[ii];
                }
                sum=sum/8.0;
                for(int jj=0;jj<8;jj++){
                    if(a[jj]>sum)
                        b[jj]=1;
                    else
                        b[jj]=0;
                }
                int s=0;
                for(int kk=0;kk<8;kk++){
                    s+=pow(2,7-kk)*b[kk];
                }

                  // memset(str1,0,sizeof(str1));
               str1[j]=char(s);

            }
            str1[k]='';
            ans+=find(str1);
         }
            printf("%d
",ans);


        }
            return 0;
}
原文地址:https://www.cnblogs.com/13224ACMer/p/4703536.html