Pagini recente » Cod sursa (job #1922197) | Cod sursa (job #863611) | Cod sursa (job #911799) | Cod sursa (job #1804691) | Cod sursa (job #932177)
Cod sursa(job #932177)
#include<cstdio>
#include<iostream>
#include<string>
#include<set>
using namespace std;
#define mp make_pair
set<pair<string,int> > Q;
set<pair<string,int> >::iterator it;
int tip;
char w[21];
int comp(string a , string b)
{
int i;
for(i = 0 ; a[i] && b[i] && a[i]==b[i] ; i++);
return i;
}
int main()
{
freopen("trie.in" , "r" , stdin );
freopen("trie.out" , "w" , stdout);
while(scanf("%d %s\n" , &tip ,w)>0)
{
if(tip == 0)
{
it = Q.lower_bound(mp(w,0));
if( it!=Q.end()&&it->first == w )
{
int nr = it->second;
Q.erase(it);
Q.insert(mp(w,nr+1));
}
else Q.insert(mp(w,1));
}
if(tip == 1)
{
it = Q.lower_bound(mp(w,0));
if(it->second==1)Q.erase(it);
else{
int nr = it->second;
Q.erase(it);
Q.insert(mp(w,nr-1));
}
}
if(tip==2)
{
it = Q.lower_bound(mp(w,0));
if(it->first == w)printf("%d\n" , it->second);
else printf("0\n");
}
if(tip==3)
{
int p1=0,p2 = 0;
it = Q.lower_bound(mp(w,0));
if(it!=Q.end())p1=comp(w,it->first);
if(it != Q.begin())
{
it--;
p2 = comp(w,it->first);
}
printf("%d\n" , max(p1 , p2 ));
}
}
return 0;
}