Pagini recente » Cod sursa (job #1229293) | Cod sursa (job #1080235) | Cod sursa (job #1844639) | Cod sursa (job #198943) | Cod sursa (job #451154)
Cod sursa(job #451154)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 9, 2010, 8:44 AM
*/
#include <cstdlib>
#include <fstream>
#include <algorithm>
/*
*
*/
using namespace std;
typedef char* str;
class trie
{
trie* t[30];
int CountA, CountS;
inline bool IsNC( char x )
{
return x < 'a' || x > 'z';
}
inline int GetV( char x )
{
return x-'a';
}
public:
trie( void ) : CountA(0), CountS(0)
{
for( int i=0; i < 30; ++i )
t[i]=NULL;
}
inline void Add( str );
inline bool Del( str );
inline int CountWordA( str );
inline int MaxLongPrefix( str, int& );
} *root;
inline void trie::Add( str s )
{
if( IsNC(s[0]) )
{
++this->CountA;
return;
}
int c=GetV(s[0]);
if( NULL == this->t[c] )
this->t[c]=new trie, ++this->CountS;
this->t[c]->Add( s+1 );
}
inline bool trie::Del( str s )
{
if( IsNC(s[0]) )
{
--this->CountA;
}
else {
int c=GetV(s[0]);
if( this->t[c]->Del(s+1) )
{
--this->CountS;
this->t[c]=NULL;
}
}
if( 0 == this->CountS && 0 == this->CountA )
return true;
return false;
}
inline int trie::CountWordA( str s )
{
if( IsNC(s[0]) )
return this->CountA;
int c=GetV(s[0]);
if( NULL == this->t[c] )
return 0;
return this->t[c]->CountWordA( s+1 );
}
inline int trie::MaxLongPrefix( str s, int& l )
{
if( IsNC(s[0]) )
return l;
int c=GetV(s[0]);
if( NULL == this->t[c] )
return l;
++l;
return this->t[c]->MaxLongPrefix( s+1, l );
}
int main(int argc, char** argv)
{
int i;
char s[30];
s[0]=NULL;
ifstream in( "trie.in" );
ofstream out( "trie.out" );
root=new trie;
while( in>>i>>s )
{
switch(i)
{
case 0 : root->Add(s); break;
case 1 : root->Del(s); break;
case 2 : out<<root->CountWordA(s)<<'\n'; break;
case 3 : i=0, out<<root->MaxLongPrefix(s, i)<<'\n';
}
}
return (EXIT_SUCCESS);
}