Cod sursa(job #2308247)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 26 decembrie 2018 18:28:42
Problema Trie Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
//#include <bits/stdc++.h>
#include <fstream>
#include <vector>
#include <bitset>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <math.h>
#include <iomanip>
#include <stack>
#include <string.h>
#include <set>
 
using namespace std;
 
ifstream in("trie.in");
ofstream out("trie.out");

const int sigma = 30;
const int DIM = 1e5 + 17;

int to[DIM][sigma];

int k = 0;

int apps [DIM * sigma];
int prefix [DIM * sigma];


void add(string word)
{
	int nod = 0;
	
	for(int i = 0; i < word.size(); i++)
	{
		if(to[nod][word[i] - 'a'] == 0)
			to[nod][word[i] - 'a'] = ++k;
		
		nod = to[nod][word[i] - 'a'];
		
		prefix[nod]++;
	}
	
	apps[nod]++;
}

void del(string word)
{
	int nod = 0;
	
	for(int i = 0; i < word.size(); i++)
	{
		nod = to[nod][word[i] - 'a'];
		prefix[nod]--;
	}
	
	apps[nod]--;
}

void printApps(string word)
{
	int nod = 0;
	
	for(int i = 0; i < word.size(); i++)
	{
		nod = to[nod][word[i] - 'a'];
		
		if(!prefix[nod])
		{
			out << 0 << '\n';
			return ;
		}
	}
	
	out << apps[nod] << '\n';
}

void printLen(string word)
{
	int nod = 0;
	
	for(int i = 0; i < word.size(); i++)
	{
		nod = to[nod][word[i] - 'a'];
		
		if(!prefix[nod])
		{
			out << i << '\n';
			return ;
		}
	}
	
	out << word.size() << '\n';
}

int main()
{
	int op;
	string word;
	
	while(in >> op >> word)
	{
		switch(op)
		{
			case(0) : add(word);       break;
			case(1) : del(word);       break;
			case(2) : printApps(word); break;
			case(3) : printLen(word);  break;
		}
	}
}