Pagini recente » Cod sursa (job #1989534) | Cod sursa (job #414876) | Cod sursa (job #1822544) | Cod sursa (job #337838) | Cod sursa (job #414890)
Cod sursa(job #414890)
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 570000
vector < vector< int > > trie;
int nr[maxn];
int nr2[maxn];
int help[26];
int nr_nodes, rez;
void add( char *sir, int where, int how_muci)
{
//nr[ where] += how_muci;
nr2[ where] += how_muci;
if( sir[0] > 'z' || sir[0] < 'a') {nr[ where] += how_muci;return;}
if( trie[ where ][ sir[0] - 'a' ] == -1)
{
vector< int > a;
for( int i = 1; i <= 26; ++i) a.push_back(-1);
trie.push_back(a);
trie[ where ][ sir[0] - 'a' ] = ++nr_nodes;
}
add( sir + 1, trie[ where ][ sir[0] - 'a' ], how_muci);
}
void deleter( char *sir, int where)
{
nr2[ where]--;
if( sir[0] > 'z' || sir[0] < 'a') {
nr[ where]--;
if( nr2[where] == 0) trie[where].erase( trie[where].begin(), trie[where].end());
return;
}
int x = trie[ where ][ sir[0] - 'a' ];
if( nr2[where] == 0) trie[where].erase( trie[where].begin(), trie[where].end());
deleter( sir + 1, x);
}
int cate( char *sir, int where)
{
if( sir[0] > 'z' || sir[0] < 'a') return nr[ where];
if( trie[ where ][ sir[0] - 'a' ] == -1)
{
return 0;
}
return cate( sir + 1, trie[ where ][ sir[0] - 'a' ]);
}
int pref( char *sir, int where, int sol)
{
if( nr2[where ] == 0)
{
if( sol != 0)
return sol-1;
return 0;
}
if( trie[ where ][ sir[0] - 'a' ] == -1)
return sol;
return pref( sir + 1, trie[ where ][ sir[0] - 'a' ], sol + 1);
}
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
char sir[22];
int n;
vector< int > a;
for( int i = 1; i <= 26; ++i) a.push_back(-1);
trie.push_back(a);
memset(sir,0,sizeof(sir));
while( scanf("%d %s",&n, sir) == 2)
{
if( n == 0)
add( sir, 0, 1);
if( n == 1)
deleter( sir, 0);
if( n == 2)
printf("%d\n",cate( sir, 0));
if( n == 3)
printf("%d\n",pref( sir, 0, 0));
}
return 0;
}