简单字典树,我看错了题目,郁闷了好一会,沙茶了一把。(次次沙茶QAQ)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2072
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 30
struct Trie{
Trie * next[maxn];
int v;
};
Trie * root;
int con;
void Init_Trie(){
root = (Trie *)malloc(sizeof(Trie));
for(int i=0;i<maxn;i++){
root -> next[i] = NULL;
}
}
void Add_Trie(char * str){
Trie * p = root , * q;
for(int i=0;str[i]!='\0';i++){
int id = str[i] - 'a';
if(p->next[id]==NULL){
q = (Trie *)malloc(sizeof(Trie));
q -> v = 0;
for(int j=0;j<maxn;j++){
q -> next[j] = NULL;
}
p -> next[id] = q;
p = p -> next[id];
}
else {
p = p -> next[id];
}
}
if(!(p->v)){
con++;
p -> v = 1;
}
}
void Del_Trie(Trie * p){
for(int i=0;i<maxn;i++){
if(p->next[i])Del_Trie(p->next[i]);
}
free(p);
}
void Input(){
char a[10005];
while(gets(a),a[0]!='#'){
con = 0;
Init_Trie();
char b[105];
int i = 0;
while(a[i]==' ')i++;
for(;i<strlen(a);){
sscanf(a+i,"%s",b);
i+=strlen(b);
while(a[i]==' ')i++;
Add_Trie(b);
}
printf("%d\n",con);
Del_Trie(root);
}
}
void File(){
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main(void){
// File();
Input();
return 0;
}