题意:判断前缀
思路:字典树的应用
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
int count;
node *trie[26];
}*a;
void init(){
a = new node;
for (int i = 0; i < 26; i++)
a->trie[i] = NULL;
}
void insert(char *str){
int len,cur;
node *head = a;
len = strlen(str);
for (int i = 0; i < len; i++){
cur = (int)(str[i]-97);
if (head->trie[cur] == NULL){
head->trie[cur] = new node;
head = head->trie[cur];
head->count = 1;
for (int j = 0; j < 26; j++)
head->trie[j] = NULL;
}
else {
head = head->trie[cur];
head->count++;
}
}
}
int search(char *str){
int len,cur;
node *head = a;
len = strlen(str);
for (int i = 0; i < len; i++){
cur = (int) (str[i]-97);
if (head->trie[cur] == NULL)
return 0;
else
head = head->trie[cur];
}
return head->count;
}
int main(){
char str[11],que[11];
int temp,query,count;
init();
while (gets(str) && strcmp(str,"") != 0)
insert(str);
while (scanf("%s",que) != EOF){
count = search(que);
printf("%d\n",count);
}
return 0;
}