开始的时候没有用字典树WA了好多次,学了之后马上就A了,哈哈哈哈爽!
#include <iostream>
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
using namespace std;
const int MAX = 26;
struct Tree{
struct Tree *next[MAX];
int number;
};
void init( struct Tree *t ){
for( int j = 0; j < MAX; j++ ){
t->next[j] = 0;
}
t->number = 0;
}
void add( struct Tree *t, char a[] ){
int length = strlen( a );
struct Tree *p = t;
for( int i = 0; i < length; i++ ){
if( p->next[a[i]-'a'] == 0 ){
p->next[a[i]-'a'] = (struct Tree*)malloc( sizeof( struct Tree ) );
p = p->next[a[i]-'a'];
for( int j = 0; j < MAX; j++ ){
p->next[j] = 0;
}
p->number = 1;
}else{
p = p->next[a[i]-'a'];
p->number++;
}
}
}
int find( struct Tree *t, char a[] ){
int length = strlen( a );
struct Tree *p = t;
for( int i = 0; i < length; i++ ){
if( p->next[a[i]-'a'] == 0 ){
return 0;
}else{
p = p->next[a[i]-'a'];
}
}
return p->number;
}
int main(){
struct Tree t;
char a[111];
init(&t);
for( int i = 0; ; i++ ){
gets(a);
if( strcmp( a, "" ) == 0 ){
break;
}
add( &t, a );
}
for( ;; ){
if( gets(a) == 0 ){
break;
}
printf( "%d\n", find( &t, a ) );
}
}