裸的字典树 复习一下
然而一开始还是敲错了细节
一个月以后的区域赛慌啊23333
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAX 26
typedef struct Trie_node{
int count;
struct Trie_node* next[26];
bool exist;
}TrieNode;
TrieNode map[10000000];
int num = 0;
TrieNode *createTrieNode(){
TrieNode *temp = &map[num++];
temp->count = 1;
for(int i=0;i<MAX;i++){
temp->next[i]=NULL;
}
return temp;
}
void insert(TrieNode **root,const char *str){
TrieNode *temp =*root;
int i=0,k;
while(str[i]){
k=str[i]-'a';
if(temp->next[k]){
temp->next[k]->count++;
}else{
temp->next[k]=createTrieNode();
}
temp = temp ->next[k];
i++;
}
}
int search(TrieNode *root,char *str){
if(root==NULL)
return 0;
TrieNode *temp = root;
int i=0,k;
while(str[i]){
k=str[i]-'a';
if(temp->next[k]){
temp=temp->next[k];
}else return 0;
i++;
}
return temp->count;
}
int main(){
TrieNode *root = createTrieNode();
char str[20];
while(gets(str),strcmp(str,""))
insert(&root,str);
while(gets(str)){
printf("%d\n",search(root,str));
}
return 0;
}