#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct T
{
T *ch[26];
int n;
}rt;
char in[30];
void init( T *t )
{
t -> n = 0;
for( int i = 0; i < 26; ++i )
t -> ch[ i ] = NULL;
}
void insert( T *t,char *in )
{
if( t -> ch[ *in - 'a' ] == NULL )
{
t -> ch[ *in - 'a' ] = ( T * )malloc( sizeof( T ) );
init( t -> ch[ *in - 'a' ] );
}
++( t -> ch[ *in - 'a' ] -> n );
if( *( in + 1 ) )
insert( t -> ch[ *in - 'a' ] , in + 1 );
}
int find( T *t, char *in )
{
if( t -> ch[ *in - 'a' ] != NULL )
{
if( *( in + 1 ) == 0 )
return t -> ch[ *in - 'a' ] -> n;
else
return find( t -> ch[ *in - 'a' ], in + 1 );
}
else
return 0;
}
int main( )
{
while( gets( in ) )
{
if( !strlen( in ) )
break;
insert( &rt,in );
}
while( scanf( "%s",in ) != EOF )
{
printf( "%d\n",find( &rt,in ) );
}
return 0;
}
此题用字典树做,很快,据小白说还有一种方法:把每个单词的第一个字母存进去,然后再进行字符串比较,这样刚好不超时
转载于:https://www.cnblogs.com/Lvsi/archive/2011/03/14/1984245.html