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;
}