Pagini recente » Cod sursa (job #2139661) | Cod sursa (job #2614386) | Cod sursa (job #1204651) | Cod sursa (job #2368523) | Cod sursa (job #2884227)
#include <bits/stdc++.h>
using namespace std;
const int SIGMA = 26;
const int NMAX = 1e5;
const int ROOT = 0;
struct trie {
trie* link[SIGMA] = {};
int aparitii;
int prefix;
};
trie* root = new trie;
void add( trie* root, char* s ) {
if ( *s == NULL ) {
root->aparitii ++;
} else {
if ( root->link[*s - 'a'] == NULL )
root->link[*s - 'a'] = new trie;
root->link[*s - 'a']->prefix ++;
add( root->link[*s - 'a'], s + 1 );
}
}
void del( trie* root, char* s ) {
if ( *s == NULL ) {
root->aparitii --;
} else {
root->link[*s - 'a']->prefix --;
del( root->link[*s - 'a'], s + 1 );
}
}
int cnt( trie* root, char* s ) {
if ( *s == NULL ) return root->aparitii;
if ( root->link[*s - 'a'] == NULL ) return 0;
return cnt( root->link[*s - 'a'], s + 1 );
}
int lcp( trie* root, char* s, int len ) {
if ( *s == NULL ) return len;
if ( root->link[*s - 'a'] == NULL || root->link[*s - 'a']->prefix == 0 ) return len;
return lcp( root->link[*s - 'a'], s + 1, len + 1 );
}
int main() {
ifstream fin( "trie.in" );
ofstream fout( "trie.out" );
int cer;
char s[30];
while ( fin >> cer >> s ) {
switch ( cer ) {
case 0:
add( root, s );
break;
case 1:
del( root, s );
break;
case 2:
fout << cnt( root, s ) << '\n';
break;
case 3:
fout << lcp( root, s, 0 ) << '\n';
break;
}
}
return 0;
}