Cod sursa(job #449176)

Utilizator alexandru92alexandru alexandru92 Data 5 mai 2010 20:49:49
Problema Trie Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
/* 
 * 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;
map< string, int > m;
map< string, int >::iterator it;
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;
    ifstream in( "trie.in" );
    ofstream out( "trie.out" );
    while( in>>i>>S )
    {
        if( !i )
            ++m[S];
        else if( 1 == i )
             {
                    it=m.find(S);
                    --it->second;
                    if( 0 == it->second )
                      m.erase(S); 
             }
             else if( 2 == i )
                  {
                        it=m.find(S);
                        if( m.end() == it )
                            out<<"0\n";
                        else out<<it->second<<'\n';
                  }
                  else {
                            it=m.lower_bound(S);
                            if( it == m.end() )
                                --it;
                            l=CPrefix( it->first );
                            if( it != m.begin() )
                                --it, l=max( l, CPrefix( it->first ) );
                            out<<l<<'\n';
                       }
    }
    return (EXIT_SUCCESS);
}