Nested map STL C++

/*
structure  of map:

-------------------------------------------------------------------
|      int             |   string        |      int                        |
|Length of string| input string  |  Repeted number  |
-------------------------------------------------------------------

Input:
ab
abcdef
ab
d
ab
ab
ab

Then, map's structure:
-------------------------------------
| 1   |      d        |          1      |
| 2   |     ab       |          5      |
| 6   |   abcdef   |         1      |
-------------------------------------
*/


#include<iostream>
#include<map>
#include <string>
#include<algorithm>
#include<utility>

using namespace std;

typedef map<string,int> innermap;
typedef map<int,innermap> outermap;
int main ()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout); 
string str,temp;
int i,j,len;
outermap mymap;
innermap::iterator iit;
outermap::iterator oit;
while(cin>>str)
{
len=str.size();
oit=mymap.find(len);
if(oit!=mymap.end())
{
innermap &inmap=oit->second;
iit=inmap.find(str);
if(iit==inmap.end())
inmap[str]=1;
else
  iit->second++;
}
else
mymap[len].insert(make_pair<string,int>(str,1));
}
for(oit=mymap.begin();oit!=mymap.end();oit++)
     for(iit=oit->second.begin();iit!=oit->second.end();iit++)
         cout<<oit->first<<" "<<iit->first<<" "<<iit->second<<endl;
    return 0;
}

Input:

       ab
      abcdef
      ab
      ab
      d
      ab
     abcd
     ab

Output:

   1 d 1
   2 ab 5
   4 abcd 1
   6 abcdef 1

Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System