Cod sursa(job #967410)

Utilizator dropsdrop source drops Data 27 iunie 2013 16:57:13
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <deque>
#include <list>
#include <ctime>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream ff("trie.in");
ofstream gg("trie.out");

struct tri{ int p, k; struct tri* f[26];
	tri(){ p=k=0; memset(f,0,sizeof(f)); }
}*t;

void add(const string& ss){
	int l=ss.length(), c;
	tri* u=t;
	for(int i=0;i<l;i++){
		c=ss[i]-'a';
		if(u->f[c]==0)u->f[c]=new tri();
		u=u->f[c];
		u->p++;
	}
	u->k++;
}

void rem(const string& ss){
	int l=ss.length(), c;
	tri* u=t;
	for(int i=0;i<l;i++){
		c=ss[i]-'a';
		u=u->f[c];
		u->p--;
	}
	u->k--;
}

int app(const string& ss){
	int l=ss.length(), c;
	tri* u=t;
	for(int i=0;i<l;i++){
		c=ss[i]-'a';
		if(u->f[c]==0)return 0;
		u=u->f[c];
	}
	return u->k;
}

int pre(const string& ss){
	int l=ss.length(), c;
	tri* u=t;
	for(int i=0;i<l;i++){
		c=ss[i]-'a';
		if(u->f[c]==0 || u->f[c]->p==0)return i;
		u=u->f[c];
	}
	return l;
}

int main(){
	int o;
	string ss;
	t=new tri();
	while(ff >> o >> ss){
		if(o==0)add(ss); else
		if(o==1)rem(ss); else
		if(o==2) gg << app(ss) << "\n"; else
			gg << pre(ss) << "\n";
	}
	return 0;
}