Cod sursa(job #226601)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 2 decembrie 2008 01:55:12
Problema Trie Scor Ascuns
Compilator cpp Status done
Runda Marime 1.25 kb
#include <stdio.h>
#include <string>
#include <set>

using namespace std;

#define maxl 30
#define maxn 100010
#define mp make_pair
#define ff first
#define ss second

int N, n;
char cuv[maxl];
int nr[maxn];
set < pair <string, int> > S;

int cmp(string s1, string s2)
{
	int i, l1, l2;
	l1 = s1.size(), l2 = s2.size();

	for (i=0; i<min(l1, l2) && s1[i]==s2[i]; i++);

	return i;
}

int main()
{
	freopen("trie.in", "r", stdin);
	freopen("trie.out", "w", stdout);

	int tip, v1, v2;
	set < pair <string, int> > :: iterator I;
	string w = "";
	w += 'z' + 1;

	S.insert( mp(w, 0) );

	while (!feof(stdin))
	{
		scanf("%d %s ", &tip, cuv);

		w = string(cuv);

		if (tip == 0)
		{
			I = S.lower_bound( mp(w, 0));

			if (I -> ff == w) nr[I -> ss]++;
			else {
					 S.insert( mp( w, ++N));
					 nr[N] = 1;
				 }
		}

		if (tip == 1)
		{
			I = S.lower_bound( mp( w, 0));
			nr[I -> ss]--;
			if (nr[I -> ss] == 0) S.erase(I);
		}

		if (tip == 2)
		{
			I = S.lower_bound( mp( w, 0));
			if (I -> ff == w) printf("%d\n", nr[I -> ss]);
			else printf("0\n");
		}

		if (tip == 3)
		{
			v1 = v2 = 0;
			I = S.lower_bound( mp( w, 0));

			v1 = cmp(I->ff, w);

			if (I != S.begin())
			{
				I--;
				v2 = cmp(I->ff, w);
			}

			printf("%d\n", max(v1, v2));
		}
	}

	return 0;
}