Cod sursa(job #1181618)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 3 mai 2014 12:53:57
Problema Trie Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.96 kb
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <bitset>
#include <ctime>
#include <cassert>
#include <iomanip>

using namespace std;


const string file = "trie";
const string inputF = file + ".in";
const string outputF = file + ".out";

const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long 
#define MOD1 666013
#define MOD2 666019
#define MOD3 1999999973
#define base 73

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;

#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end()) 
#define sz(a) int((a).size()) 
#define pb push_back 
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)

class Trie {
public:
	int nr, sum;
	vector < pair<Trie*, int> > lit;
	Trie() : nr(0), sum(0), lit(26, mp((Trie*)0, 0)) {}	
};

Trie *rootTrie;
void insert(Trie *root, char *s);
void erase(Trie *root, char *s);
int count(Trie *root, char *s);
int prefix(Trie *root, char *s, int lg);

int main() {
#ifndef INFOARENA
	freopen("input.txt", "r", stdin);
#else 
	freopen(inputF.c_str(), "r", stdin);
	freopen(outputF.c_str(), "w", stdout);
#endif
	int cod;
	char S[21];
	rootTrie = new Trie();
	rootTrie -> nr = -1;
	while (scanf("%d %s\n", &cod, S) > 0) {
		if (cod == 0) {
			insert(rootTrie, S);
		}
		else if (cod == 1) {
			erase(rootTrie, S);
		}
		else if (cod == 2) {
			printf("%d\n", count(rootTrie, S));
		}
		else {
			printf("%d\n", prefix(rootTrie, S, 0));
		}
	}
	return 0;
}

void insert(Trie *root, char *s) {
//cout << "insert " << s << '\n';
	root -> sum++;
	if (s[0] == '\0') {
		root -> nr++;
	}
	else {
		int pos = s[0] - 'a';
		if (root -> lit[pos].first == 0) {
			root -> lit[pos].first = new Trie();
		}
		root -> lit[pos].second++;
		insert(root -> lit[pos].first, s + 1);
	}
}

void erase(Trie *root, char *s) {
//cout << "erase " << s << '\n';
	root -> sum--;
	if (s[0] == '\0') {
		root -> nr--;
	}
	else {
		int pos = s[0] - 'a';
		erase(root -> lit[pos].first, s + 1);
		root -> lit[pos].second --;
	}
	if (root -> sum == 0 && root -> nr >= 0) {
		delete root;
	}
}

int count(Trie *root, char *s) {
//cout << "count " << s << '\n';
	if (s[0] == '\0') {
		return root -> nr;
	}
	if (root -> lit[s[0] - 'a'].first != 0) {
		return count(root->lit[s[0] - 'a'].first, s + 1);
	}
	return 0;
}

int prefix(Trie *root, char *s, int lg) {
//cout << "prefix " << s << '\n';
	if (root -> lit[s[0] - 'a'].second < 1) {

		return lg;
	}
	return prefix(root -> lit[s[0] - 'a'].first, s + 1, lg + 1);
}