Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <set>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <stack>
- #include <sstream>
- #include <cmath>
- #include <cstdlib>
- #include <cctype>
- #define clr( x , y ) memset( x ,y , sizeof x )
- #define FOR(i,A) for(typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
- #define mp make_pair
- #define TAM 100
- #define MOD 1000000007
- #define debug( x ) cout << #x << " = " << x << endl
- #define f(i,n) for(int i = 0 ; i < n ; i++)
- #define ff(i,a,b) for(int i = a ; i < b ; i++)
- #define all( x ) x.begin() , x.end()
- #define ral( x ) x.rbegin() , x.rend()
- using namespace std ;
- typedef pair<int,int> ii ;
- typedef pair<ii,ii> pii ;
- typedef long long ll ;
- typedef long double ld ;
- string alpha ;
- struct Trie{
- Trie * next[ 300 ] ;
- int words ;
- int prefix ;
- Trie(){
- init() ;
- }
- void init(){
- for(int i = 0 ; i < 300 ; i++) next[ i ] = NULL ;
- words = prefix = 0 ;
- alpha = "" ;
- }
- char toDict( char c ){
- if( !alpha.find( c ) ) alpha += c ;
- return c ;
- }
- void add( string S ){
- Trie * nodo = this ;
- for(int i = 0 ; i < S.size() ; i++){
- char c = toDict( S[ i ] ) ;
- if( nodo->next[ c ] == NULL ) nodo->next[ c ] = new Trie ;
- nodo->prefix++ ;
- nodo = nodo->next[ c ] ;
- }
- nodo->words++ ;
- nodo->prefix++ ;
- printf("\tAñadido \"%s\"\n" , S.c_str() ) ;
- }
- int countWords( string S ){
- Trie * nodo = this ;
- for(int i = 0 ; i < S.size() ; i++){
- char c = S[ i ] ;
- if( nodo->next[ c ] == NULL ) return 0 ;
- nodo = nodo->next[ c ] ;
- }
- return nodo->words ;
- }
- int countPrefix( string S ){
- Trie * nodo = this ;
- for(int i = 0 ; i < S.size() ; i++){
- char c = S[ i ] ;
- if( nodo->next[ c ] == NULL ) return 0 ;
- nodo = nodo->next[ c ] ;
- }
- return nodo->prefix ;
- }
- } T ;
- int main(){
- T.init() ;
- char c , S[ TAM ] ;
- while( scanf("%c %s" , &c , S ) == 2 ){
- cin.ignore() ;
- if( c == 'A' )
- T.add( S ) ;
- else if( c == 'C' )
- printf("#pal( %s ) = %d\n" , S , T.countWords( S ) ) ;
- else if( c == 'P' )
- printf("#prefix( %s ) = %d\n" , S , T.countPrefix( S ) ) ;
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement