字典树基础,没有什么难度,开始学习字典树的时候看着代码敲一遍就可以了,没有什么深入思考的地方(要会链表,要会链表,要会链表)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct node{
int cnt;
struct node *next[26];
node(){
cnt = 0;
for(int i = 0; i < 26; ++i)
next[i] = NULL;
}
};
void clear_root(node *p){
for(int i = 0; i < 26; ++i)
if(p->next[i] != NULL)
clear_root(p->next[i]);
delete p;
}
int main()
{
char s[11];
node *root = new node;
while(gets(s)){
if(strcmp(s, "") == 0)
break;
node *p = root;
for(unsigned i = 0; i < strlen(s); ++i){
int t = s[i] - 'a';
if(p->next[t] == NULL){
p->next[t] = new node;
}
p = p->next[t];
p->cnt++;
}
}
while(scanf("%s", s) != EOF){
int ok = 1;
node *p = root;
for(unsigned i = 0; i < strlen(s); ++i){
int t = s[i] - 'a';
if(p->next[t] == NULL){
printf("0\n");
ok = 0;
break;
}
p = p->next[t];
}
if(ok)
printf("%d\n", p->cnt);
}
clear_root(root);
return 0;
}