Cod sursa(job #2094354)

Utilizator ArctopusKacso Peter-Gabor Arctopus Data 25 decembrie 2017 18:46:53
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string>


using namespace std;

const int NLIM = 3e5 + 10;

vector< int > graph[NLIM];
int graphSize = 1;
char nodChar[NLIM];
int nodCnt[NLIM];
int parent[NLIM];
string s;
int gi = 0;


void add( )
{
	int nod = 0;
	for( int i = 0; i < s.size(); ++i )
	{
		// find next node:
		bool found = 0;
		for( int j = 0; j < graph[nod].size() && !found; ++j )
		{
			int nnod = graph[nod][j];
			if( nodChar[nnod] == s[i] )
			{
				nod = nnod;
				found = 1;
			}
		}
		if( !found )
		{
			// add new node
			graph[nod].push_back( graphSize );
			nodChar[graphSize] = s[i];
			parent[graphSize] = nod;
			nod = graphSize;
			++graphSize;
		}
	}
	++nodCnt[nod];
}

void del( )
{
	int nod = 0;
	for( int i = 0; i < s.size(); ++i )
	{
		// find next node:
		bool found = 0;
		for( int j = 0; j < graph[nod].size() && !found; ++j )
		{
			int nnod = graph[nod][j];
			if( nodChar[nnod] == s[i] )
			{
				nod = nnod;
				found = 1;
			}
		}
	}

	
	// nod
	nodCnt[nod] -= 1;
	while( nodCnt[nod] == 0 && graph[nod].size() == 0 && nod != 0 )
	{

		int pnod = parent[nod];
		// find and delete nod:
		bool found = 0;
		for( int j = 0; j < graph[pnod].size() && !found; ++j )
		{
			int nnod = graph[pnod][j];
			if( nnod == nod )
			{
				// delete node from graph
				graph[pnod].erase( graph[pnod].begin() + j );
				found = 1;
			}
		}
		nod = pnod;
	}
	
}

int cnt( )
{
	int nod = 0;
	for( int i = 0; i < s.size(); ++i )
	{
		// find next node:
		bool found = 0;
		for( int j = 0; j < graph[nod].size() && !found; ++j )
		{
			int nnod = graph[nod][j];
			if( nodChar[nnod] == s[i] )
			{
				nod = nnod;
				found = 1;
			}
		}

		if( !found ) 
		{
			nod = 0;
			break;
		}
	}

	return nodCnt[nod];
}

int pref()
{
	int nod = 0;
	int len = 0;
	for( int i = 0; i < s.size(); ++i )
	{
		// find next node:
		bool found = 0;
		for( int j = 0; j < graph[nod].size() && !found; ++j )
		{
			int nnod = graph[nod][j];
			if( nodChar[nnod] == s[i] )
			{
				nod = nnod;
				found = 1;
			}
		}
	
		if( found )
			len = i + 1;
	

		if( !found ) 
			break;
	}
	return len;
}



ifstream fcin("trie.in");
ofstream fcout("trie.out");

int main()
{
	nodChar[0] = '-';

	while( !fcin.eof() )
	{
		int t = -2;

		fcin >> t >> s;

		//if( gi % 100 == 0 )
			
		++gi;
		//cout << gi << "\n";
		//if( gi == 57986 )
			//cout << t << " " << s <<"\n";
		if( t == -2 )
			break;
		
		if( t == 0 )
		{
			add();
		}
		else if( t == 1 )
		{
			del();
		}
		else if( t == 2 )
		{
			fcout << cnt() << "\n";
		}
		else
		{
			fcout << pref() << "\n";
		}

	}

	/*/
	for( int i = 0; i < graphSize; ++i )
	{
		cout << i << "(" << nodChar[i] << "): ";
		for( int j = 0; j < graph[i].size(); ++j )
		{
			cout << graph[i][j] << "(" << nodChar[graph[i][j]] << ") ";
		}
		cout << "\n";
	}
	//*/
	
}