字典树
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node {
int next[ 26 ] ;
int v;
bool IsWord;
void init ( ) {
v= 0 ;
IsWord= false;
memset ( next, - 1 , sizeof ( next) ) ;
}
} a[ 1000005 ] ;
int tol= 0 ;
void Insert ( char s[ ] , int len) {
int now= 0 ;
for ( int i= 0 ; i< len; i++ ) {
int tmp= s[ i] - 'a' ;
if ( a[ now] . next[ tmp] == - 1 ) {
a[ now] . next[ tmp] = ++ tol;
a[ tol] . init ( ) ;
}
now= a[ now] . next[ tmp] ;
a[ now] . v++ ;
}
a[ now] . IsWord= true;
}
int Query_Pre ( char s[ ] , int len) {
int now= 0 ;
for ( int i= 0 ; i< len; i++ ) {
int tmp= s[ i] - 'a' ;
if ( a[ now] . next[ tmp] == - 1 )
return 0 ;
now= a[ now] . next[ tmp] ;
}
return a[ now] . v;
}
bool Query_IsWord ( char s[ ] , int len) {
int now= 0 ;
for ( int i= 0 ; i< len; i++ ) {
int tmp= s[ i] - 'a' ;
if ( a[ now] . next[ tmp] == - 1 )
return false;
now= a[ now] . next[ tmp] ;
}
return a[ now] . IsWord;
}
char s[ 100 ] ;
int main ( ) {
int k;
a[ 0 ] . init ( ) ;
string op;
while ( 1 ) {
cin>> op;
if ( op== "end" ) {
break ;
}
if ( op== "add" ) {
scanf ( "%s" , s) ;
Insert ( s, strlen ( s) ) ;
} else if ( op== "pre" ) {
scanf ( "%s" , s) ;
printf ( "%d\n" , Query_Pre ( s, strlen ( s) ) ) ;
} else if ( op== "wor" ) {
scanf ( "%s" , s) ;
if ( Query_IsWord ( s, strlen ( s) ) ) {
printf ( "Yes\n" ) ;
} else {
printf ( "No\n" ) ;
}
}
}
return 0 ;
}