杭電3724

Encoded Barcodes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1508    Accepted Submission(s): 530


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 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
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;當把數的平均值求出來,小於的就爲0,大於的就爲1.
#include <cstdio>
#include <cstring>
#include <cstdlib>
struct node
{
    node *x[26];
    int cnt;
};
node *tree;
int n,m,k;
char cha[50];
char stu[50];
double p[50];
int calculate(int t)//計算二進制
{
    int i,sum=1;
    for(i=1;i<=t;++i)
        sum*=2;
    return sum;
}
void create(node *&tree)//初始化一個樹
{
    tree=(node *)malloc(sizeof(node));
    tree->cnt=0;
    for(int i=0;i<26;++i)
        tree->x[i]=NULL;
}
void insertTree()//把單詞插入樹中
{
    node *f=tree;
    int i,j,len=strlen(stu);
    for(i=0;i<len;++i)
    {
        int p=stu[i]-'a';
        if(f->x[p]==NULL)
        {
            f->x[p]=(node *)malloc(sizeof(node));
            f=f->x[p];
            f->cnt=1;
            for(j=0;j<26;++j)
                f->x[j]=NULL;
        }
        else
        {
            f=f->x[p];
            f->cnt++;
        }
    }
}
int searchTree()//在樹中查找前綴
{
  //  printf("cha=%s  %d\n",cha,strlen(cha));
    node *f=tree;
    int i,len=strlen(cha);
    for(i=0;i<len;++i)
    {
        int p=cha[i]-'a';
        if(f->x[p]==NULL)
        {
           return 0;
        }
         else
        {
            f=f->x[p];
        }
    }
    return f->cnt;
}
void deleteTree(node *&tree)
{
    int i;
    for(i=0;i<26;++i)
    {
        if(tree->x[i]!=NULL)
         deleteTree(tree->x[i]);
    }
    free(tree);
    tree=NULL;
}
int main()
{
    int i,j,o;
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        create(tree);
        for(i=1;i<=n;++i)
        {
            scanf("%s",stu);getchar();
            insertTree();
        }
        int ans=0;
        for(i=1;i<=m;++i)
        {
            scanf("%d",&k);
            int u=-1;
            for(j=1;j<=k;++j)
            {
                double ssum=0;
                for(o=1;o<=8;++o)
                {
                    scanf("%lf",&p[o]);
                    ssum+=p[o];
                }
                ssum=ssum/8;
                int sum=0;
                for(o=8;o>=1;--o)
                {
                    if(p[o]>ssum)
                     {
                         sum+=calculate(8-o);
                     }
                }
              //  printf("sum  %d\n",sum);
                ++u;
                cha[u]=sum;
            //    printf("cha[%d]=%c\n",u,cha[u]);
            }
            cha[u+1]='\0';
            ans+=searchTree();
        }
        printf("%d\n",ans);
        deleteTree(tree);
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章