hdu-3724 Encoded Barcodes

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

题意开始看了好久都没有明白,英语不好,抓狂

后来勉强懂了,就是让你查找所给单词的前缀数,首先给你n个单词,然后建成字典树,但是需要查找的字符串是由8位数字给出的,我是求出了8位数的平均数,然后大于平均数的是1 小于是0 求出对应的字母构成一个字符串,然后查找前缀,累加和。

 

#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node
{
    int count;
    struct node *next[26];
}*tree;

void insert(tree h,string s)
{
    tree p=h;
    int len=s.length(),i;
    for(i=0;i<len;i++)
    {
        int index=s[i]-'a';
        if(p->next[index]!=NULL)
        {
            p=p->next[index];
            p->count++;
        }
        else
        {
            tree tem=(tree)calloc(1,sizeof(node));
            tem->count=1;
            p->next[index]=tem;
            p=tem;
        }
    }
}
int find(tree h,string s)
{
    tree p=h;
    int len=s.length(),i;
    for(i=0;i<len;i++)
    {
        int index=s[i]-'a';
        if(p->next[index]!=NULL)
            p=p->next[index];
        else return 0;
    }
    return p->count;
}

void delet(tree head)
{
    int i;
    for(i=0;i<26;i++)
    {
        if(head->next[i]!=NULL)
            delet(head->next[i]);
    }
    free(head);
}

int main()
{
    //freopen("a.txt","r",stdin);
    int n,m,i,j,k,y,cnt;
    string s,ss;
    double sum,x[10];
    int xx[10];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt=0;
        tree head=(tree)calloc(1,sizeof(node));
        head->count=0;
        for(i=0;i<n;i++)
        {
            cin>>s;
            insert(head,s);
        }
        while(m--)
        {
        scanf("%d",&k);
        ss="";
        for(i=0;i<k;i++)
        {
            sum=0;
            y=0;
            for(j=0;j<8;j++)
            {
                scanf("%lf",&x[j]);
                sum+=x[j];
            }
            sum/=8;
            //printf("%lf\n",sum);
            for(j=0;j<8;j++)
            {
               if(x[j]>sum) xx[j]=1;
               else xx[j]=0;
              // printf("%d ",xx[j]);
            }
            //printf("\n");
            for(j=0;j<8;j++)
            {
                y+=(int)pow((double)2,7-j)*xx[j];
            }
            //cout<<y<<endl;
            ss+=(y-97+'a');
            //cout<<ss<<endl;
        }
        cnt+=find(head,ss);
        //cout<<cnt<<endl;
        }
        printf("%d\n",cnt);
        delet(head);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值