I need to speed this code at least 2 times!
Posted
by Dominating
on Stack Overflow
See other posts from Stack Overflow
or by Dominating
Published on 2010-03-28T22:21:09Z
Indexed on
2010/03/28
22:23 UTC
Read the original article
Hit count: 265
c++
include
include
include
include
using namespace std;
inline void PrintMapName(multimap pN, string s)
{
pair::iterator, multimap::iterator> ii;
multimap::iterator it;
ii = pN.equal_range(s);
multimap tmp;
for(it = ii.first; it != ii.second; ++it)
{
tmp.insert(pair(it->second,1));
}
multimap::iterator i;
bool flag = false;
for(i = tmp.begin(); i != tmp.end(); i++)
{
if(flag)
{
cout<<" ";
}
cout<first;
if(flag)
{
cout<<" ";
}
flag = true;
}
cout<
int main()
{
multimap phoneNums;
multimap numPhones;
int N;
cin>>N;
int tests;
string tmp, tmp1,tmp2;
while(N > 0)
{
cin>>tests;
while(tests > 0)
{
cin>>tmp;
if(tmp == "add")
{
cin>>tmp1>>tmp2;
phoneNums.insert(pair(tmp1,tmp2));
numPhones.insert(pair(tmp2,tmp1));
}
else
{
if(tmp == "delnum")
{
cin>>tmp1;
multimap::iterator it;
multimap::iterator tmpr;
for(it = phoneNums.begin(); it != phoneNums.end();it++)
{
tmpr = it;
if(it->second == tmp1)
{
phoneNums.erase(it,tmpr);
}
}
numPhones.erase(tmp1);
}
else
{
if(tmp == "delname")
{
cin>>tmp1;
phoneNums.erase(tmp1);
multimap::iterator it;
multimap::iterator tmpr;
for(it = numPhones.begin(); it != numPhones.end();it++)
{
tmpr = it;
if(it->second == tmp1)
{
numPhones.erase(it,tmpr);
}
}
}
else
{
if(tmp =="queryname")
{
cin>>tmp1;
PrintMapName(phoneNums, tmp1);
}
else//querynum
{
cin>>tmp1;
PrintMapName(numPhones, tmp1);
}
}
}
}
tests--;
}
N--;
}
return 0;
}
© Stack Overflow or respective owner