Pagini recente » Cod sursa (job #1803900) | Cod sursa (job #2941730) | Cod sursa (job #527621) | Cod sursa (job #2571832) | Cod sursa (job #3297019)
#include <bits/stdc++.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct trie {
trie* child[26];
int cnt;
int pass;
};
trie* root=new trie;
void add(string s)
{
trie* curr=root;
for (char c:s )
{
int i=c-'a';
if ( !curr->child[i] )
{
trie* p=new trie;
curr->child[i]=p;
}
curr=curr->child[i];
curr->pass++;
}
curr->cnt++;
}
void sterge(string s)
{
trie* curr=root;
for (char c:s )
{
int i=c-'a';
trie *next=curr->child[i];
next->pass--;
curr=next;
}
curr->cnt--;
}
int numara(string s)
{
trie* curr=root;
for (char c:s )
{
int i=c-'a';
if ( curr->child[i]==0 )
return 0;
curr=curr->child[i];
}
return curr->cnt;
}
int prefix(string s)
{
trie *curr=root;
int lg=0;
for (char c:s )
{
int i=c-'a';
if ( curr->child[i]==0 )
break;
curr=curr->child[i];
if ( curr->pass>0 )
lg++;
else break;
}
return lg;
}
int main()
{
int c;
string s;
while ( f >> c >> s )
{
if ( c==0 )
add(s);
else if ( c==1 )
sterge(s);
else if ( c==2 )
g << numara(s) << '\n';
else if ( c==3 )
g << prefix(s) << '\n';
}
return 0;
}