Pagini recente » Cod sursa (job #3160103) | Cod sursa (job #1010611) | Cod sursa (job #1010511) | Cod sursa (job #567561) | Cod sursa (job #449107)
Cod sursa(job #449107)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 5, 2010, 6:52 PM
*/
#include <map>
#include <string>
#include <cstdlib>
#include <fstream>
/*
*
*/
using namespace std;
string S;
inline int CPrefix( string P )
{
int i, l=min( S.size(), P.size() );
for( i=0; i < l && S[i] == P[i]; ++i );
return i;
}
int main(int argc, char** argv)
{
int i, l;
map< string, int > m;
map< string, int >::iterator it, iend;
ifstream in( "trie.in" );
ofstream out( "trie.out" );
while( in>>i>>S )
{
if( !i )
++m[S];
else if( 1 == i )
{
--( l=m[S] );
if( !l )
m.erase(S);
}
else if( 2 == i )
out<<m[S]<<'\n';
else {
it=m.lower_bound(S);
if( it == m.end() )
--it;
l=CPrefix( it->first );
if( it != m.begin() )
l=max( l, CPrefix( (--it)->first ) );
out<<l<<'\n';
}
}
return (EXIT_SUCCESS);
}