Friday, 2 February 2018

Sorting Bank Accounts (SBANK) in C++ | SPOJ

C++ Program for the adding reversed number problem of spoj is given below. The given solution is accepted by spoj judge. Approach is simple, we will input the account number as strings and then insert them into a container such as vector and we will also store their number of occurrence in an unordered_map<string,int> where string is the key and its frequency is value. Then we will sort the elements of container and print the sorted data along with their frequency.

PROGRAM:

#include <bits/stdc++.h>
#include <string>
#include <algorithm>
using namespace std;
int main(){
 
  int t;
  cin>>t;
  while(t--)
  {
 
       vector<string> ll;
       std::unordered_map<string,int> m;
       std::unordered_map<string,bool> status;
       int n;
       cin>>n;
       cin.ignore();
       while(n--){
 
            string s;
 
            getline(cin,s);
 
            ll.push_back(s);
            m[s]++;
            status[s]=false;
 
       }
 
       std::sort(ll.begin(),ll.end());
       for(auto it=ll.begin();it!=ll.end();it++)
       {
          if(status[*it]==false)
          {
              cout<<*it<<" "<<m[*it]<<"\n";
              status[*it]=true;
          }
       }
       cout<<"\n";
  }
  return 0;
}

OUTPUT:



EmoticonEmoticon