hdu 3724 字典树

来源:互联网 发布:微缩景观模型淘宝 编辑:程序博客网 时间:2024/05/29 08:57

CLICK HERE

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.
这道题是字典树的运用,关键在于处理条形码,可以先求出8个条形码的总和,然后取平均值,如果比平均值还大的就属于宽带,比平均值小的就属于窄带~~~
然后就是字典树模板了,而且通过做这道题的感悟有两点,一个是求平均值,另一个是自己的代码风格啊啊啊啊啊,!!!必须要好好研究,不然debug可费劲了
附上代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
const int eps=1e-9;
using namespace std;
struct node
{
  int num;
  node *next[26];
};
void Insert(char *b,node *T)
{
   node *p,*q;
   p=T;
   int len2=strlen(b);
   for(int i=0;i<len2;i++)
   {
      int id=b[i]-'a';
      if(p->next[id]==NULL)
      {
         q=(node *)malloc(sizeof(node));
         q->num=0;
         for(int j=0;j<26;j++)
           q->next[j]=NULL;
         p->next[id]=q;
      }
      p=p->next[id];
      p->num++;
   }
 }
int Search(char *a,node *T)
{
   node *p=T;
   int len=strlen(a);
   for(int i=0;i<len;i++)
   {
      int id=a[i]-'a';
      if(p->next[id]==NULL)
      {
        return 0;
      }
      p=p->next[id];
   }
   return p->num;
}
inline int pow(int b)
{
 int ans=1;
 for(int i=1;i<=b;i++)
   ans=ans*2;
 return ans;
}


int main(){
   int n,m,k;
   long long ans,cnt;
   char words[100];
   double num[8];
   double sum;
   int number;
   while(scanf("%d%d" , &n , &m) != EOF)
   {
       ans = 0;
       cnt = 0;
       node *T=(node *)malloc(sizeof(node));
       T->num=0;
        for(int i=0;i<26;i++)
         T->next[i]=NULL;
       for(int i = 0 ; i < n ; i++)
       {
          scanf("%s" , words);
          Insert(words,T);
       }
       
       for(int i = 0 ; i < m ; i++)
       {
          scanf("%d" , &k);
          memset(words , '\0' , sizeof(words));
          for(int j = 0 ; j < k ; j++)
          {
             sum = 0.0;
             for(int t = 0 ; t < 8 ; t++)
             {
                scanf("%lf" , &num[t]);
                sum += num[t];
             }
             sum /= 8;
             number = 0;
             for(int t = 0 ; t < 8 ; t++)
             {
                 if(num[t]-sum > eps)
                   number += pow(7-t);
             }
             char c = number;
             words[j] = c;
          }
          ans += Search(words,T);
       }
       printf("%lld\n" , ans);
   }
   return 0;
}

0 0
原创粉丝点击