Monday, 22 December 2014

sorting bank accounts (map)(spoj)

Its a simple question which demands use of map and good control on string .
There is a question with good combination of both and though simple offers many things to learn.

http://www.spoj.com/problems/SBANK/

#include<stdio.h>
#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
int t,p;
freopen("kr.in","r",stdin);
map <string, int> cal;
map <string, int> :: iterator it;
char st[10000];

scanf("%d",&t);
while(t--)
{
    scanf("%d",&p);
    char c;
    scanf("%c",&c);
    for(int i=0;i<p;i++){
          //  fflush(stdin);
    gets(st);
    //puts(st);
    cal[st]++;}

    for(it=cal.begin();it!=cal.end();it++)
{
   // cout<<it->first<<" ";
    printf("%s %d\n",it->first.c_str(),it->second);
}
printf("\n");
cal.clear();
}
}

things learnt 

if we use string it becomes difficult to take input via gets
anyways its imp to take a char input to remove the new line character from getting into gets
if we want to print the string which is the key via printf , we need to use it->first.c_str() , 

most importantly , we cannot take string (which is a object input through scanf or gets ) which are functions of cpp and if we use just cin we will be unable to string input with space.