Pagini recente » Cod sursa (job #1623831) | Cod sursa (job #1296857) | Cod sursa (job #1643040) | Cod sursa (job #1792089) | Cod sursa (job #1125098)
#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
map<string,int> M;
map<string,int>::iterator it,IT;
int op,maxim,i,n,c,k,m;
string w,s,s2;
int main()
{
while(!f.eof())
{
f>>op>>w;
it=M.find(w);
if (op==0)
{
if (it==M.end()) M.insert(make_pair(w,1));
else it->second=it->second+1;
}
else if (op==1)
{
if (it->second==1) M.erase(w);
else it->second=it->second-1;
}
else if (op==2)
{
if (it==M.end()) g<<0<<"\n";
else g<<it->second<<"\n";
}
else
{
if (it!=M.end())
{
s=it->first;
g<<s.length()<<"\n";
}
else
{
s=w;
maxim=0;
for(IT=M.begin();IT!=M.end();++IT)
{
s2=IT->first;
n=s2.length()-1;
m=s.length()-1;
if (n>m) k=m;else k=n;
c=0;
for(i=0;i<=k;++i)
if (s[i]==s2[i]) ++c;else break;
if (c>maxim) maxim=c;
}
g<<maxim<<"\n";
}
}
}
f.close();
g.close();
// for(it=M.begin();it!=M.end();++it)
// cout<<it->first<<" "<<it->second<<"\n";
return 0;
}