Pagini recente » Cod sursa (job #1853095) | Cod sursa (job #1770815) | Cod sursa (job #2343838) | Cod sursa (job #1498612) | Cod sursa (job #2202995)
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct trie {
int cnt;
int nfii;
trie* urm[30];
trie() {
cnt = nfii = 0;
memset(urm, 0, sizeof(urm));
}
};
trie *inc = new trie;
char s[30];
void add( trie *t, char *s)
{
int k;
while (*s != 0)
{
k = *s - 'a';
if (t -> urm[k] == 0)
t -> urm[k] = new trie, t -> nfii++;
t = t -> urm[k];
s++;
}
t -> cnt++;
}
int apar_c1(trie *t, char *s)
{
if (*s == 0)
return t -> cnt;
else
if (t -> urm[*s-'a'] != 0)
return apar_c1( t -> urm[*s-'a'] , s+1);
return 0;
}
int lung_c2(trie *t, char *s, int K)
{
if (*s == 0 || t -> urm[*s-'a'] == 0)
return K;
return lung_c2( t -> urm[*s-'a'], s+1, K+1);
}
bool Delete(trie *t, char *s)
{
if (*s == 0 || t -> cnt > 0)
t -> cnt--;
else
if (t -> urm[*s-'a'] != 0 && Delete(t -> urm[*s-'a'], s+1) )
t -> nfii--, t -> urm[*s-'a'] = 0;
if (t -> nfii == 0 && t -> cnt == 0 && t != inc) {
delete t;
return 1;
}
return 0;
}
int main()
{
while (f.getline(s, sizeof(s)))
{
if (s[0] == '0')
add(inc, s+2);
else
if (s[0] == '1')
Delete(inc, s+2);
else
if (s[0] == '2')
g << apar_c1(inc, s+2) << '\n';
else
if (s[0] == '3')
g << lung_c2(inc, s+2, 0) << '\n';
}
return 0;
}