标签:hdu 1251 统计难题 字典树
banana band bee absolute acm ba b band abc
2 3 1 0
题解:建一颗字典树,然后查询出现的次数就行了。
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<cstring>
#define N 10010
using namespace std;
int n;
char s[N];
typedef struct tire {
int num;
struct tire *net[26];
}*Tire;
void Init(Tire &p) {
p=(Tire)malloc(sizeof(tire));
p->num=0;
for(int i=0; i<26; i++) {
p->net[i]=NULL;
}
}
void CreatTire(Tire &p,char s[]) {
int i=0;
Tire q=p;
while(s[i]) {
int nx=s[i]-'a';
if(q->net[nx]==NULL) {
Init(q->net[nx]);
q->net[nx]->num=1;
} else {
q->net[nx]->num++;
}
q=q->net[nx];
i++;
}
}
int Serch(Tire p,char s[]) {
int i=0;
Tire q=p;
while(s[i]) {
int nx=s[i]-'a';
if(q->net[nx]==NULL)return 0;
q=q->net[nx];
i++;
}
return q->num;
}
int main() {
//freopen("test.in","r",stdin);
Tire p;
Init(p);
while(gets(s)!=NULL&&s[0]!='\0') {
CreatTire(p,s);
}
while(~scanf("%s",s)) {
printf("%d\n",Serch(p,s));
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:hdu 1251 统计难题 字典树
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/46909401