标签:hdu1251
banana band bee absolute acm ba b band abc
2 3 1 0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Node{
struct Node *nextAlph[26];
int num;
} root;
void insert(char *str)
{
Node *p = &root;
int id;
while(*str){
id = *str - 'a';
if(p->nextAlph[id] == NULL){
p->nextAlph[id] = (Node *)malloc(sizeof(Node));
p = p->nextAlph[id];
memset(p->nextAlph, 0, sizeof(p->nextAlph));
p->num = 0;
}else p = p->nextAlph[id];
++p->num; ++str;
}
}
int FIND(char *str)
{
Node *p = &root;
int id;
while(*str){
id = *str - 'a';
if(p->nextAlph[id] == NULL) return 0;
p = p->nextAlph[id];
++str;
}
return p->num;
}
int main()
{
//freopen("stdin.txt", "r", stdin);
char str[12];
while(gets(str), *str) insert(str);
while(gets(str) != NULL) printf("%d\n", FIND(str));
return 0;
}HDU1251 统计难题 【trie树】,布布扣,bubuko.com
标签:hdu1251
原文地址:http://blog.csdn.net/chang_mu/article/details/37875677