Cod sursa(job #383519)

Utilizator indestructiblecont de teste indestructible Data 16 ianuarie 2010 19:19:48
Problema Nums Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <stdio.h>
#include <string.h>
#include <vector>
#define LMAX 100005
#define HMAX 10
int n,l,k;
char v[LMAX],tip;
inline int cif(char x)
{
	return x>='0' && x<='9';
}
struct trie
{
	int cnt,fin;
	trie *fiu[HMAX];
	trie () 
	{
		cnt=fin=0;
		memset(fiu,0,sizeof(fiu));
	}
};
trie *T= new trie;
void ins(trie *nod,char *s)
{
	if (*s=='\n')
	{
		if (nod->fin)
			tip=1;
		else
		{
			nod->cnt++;
			nod->fin++;
		}
		return ;
	}
	nod->cnt++;
	if (nod -> fiu[*s-'0'] == NULL)
		nod -> fiu[*s-'0'] = new trie;
	ins (nod -> fiu[*s-'0'], s+1);
}
void del(trie *nod,char *s)
{
	if (*s=='\n')
		return;
	nod->cnt--;
	del(nod->fiu[*s-'0'],s+1);
}
void add()
{
	int poz=0;
	tip=0;
	while (v[poz]==' ')
		poz++;
	ins(T,v+poz);
	if (tip)
		del(T,v+poz);
}
void find(trie *nod)
{
	int i;
	for (i=0; i<=9; i++)
		if (nod->fiu[i] !=NULL)
		{
			if ((nod->fiu[i])->cnt<k)
				k-=(nod->fiu[i])->cnt;
			else
			{
				printf("%d",i);
				find(nod->fiu[i]);
				break;
			}
		}
}
int main()
{
	freopen("nums.in","r",stdin);
	freopen("nums.out","w",stdout);
	scanf("%d\n",&n);
	while (n)
	{
		scanf("%d",&l);
		if (l)
		{
			fgets(v,LMAX,stdin);
			add();
		}
		else
		{
			scanf("%d",&k);
			find(T);
			printf("\n");
		}
		n--;
	}
	return 0;
}