I am solving a problem on SPOJ and am currently unsatisfied with my submission time, given that my fastest time is 0.28s (which ranks me 123/5037) and the leading submission is 0.04s.
The problem is pretty simple: Given a list of bank account numbers, print the bank account number along with the frequency at which it is repeated.
Input
t [the number of tests <= 5]
n [the number of accounts<= 100 000][list of accounts]
[empty line]
[next test cases]Output
[sorted list of accounts with the number of repeated accounts]
[empty line]
[other results]
Example:
Input:
2
6
03 10103538 2222 1233 6160 0142
03 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0142
30 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0142
5
30 10103538 2222 1233 6160 0144
30 10103538 2222 1233 6160 0142
30 10103538 2222 1233 6160 0145
30 10103538 2222 1233 6160 0146
30 10103538 2222 1233 6160 0143
Output:
03 10103538 2222 1233 6160 0141 1
03 10103538 2222 1233 6160 0142 1
30 10103538 2222 1233 6160 0141 2
30 10103538 2222 1233 6160 0142 2
30 10103538 2222 1233 6160 0142 1
30 10103538 2222 1233 6160 0143 1
30 10103538 2222 1233 6160 0144 1
30 10103538 2222 1233 6160 0145 1
30 10103538 2222 1233 6160 0146 1
In order to solve this, I am using a map where the key is the bank account and the value is the frequency of repetition. However, I am first loading the bank accounts into a vector, and I am pretty sure this is one area where I am losing efficiency. I have tried to avoid using the vector, but my efficiency is greatly reduced.
What are some things that I can do to either improve my algorithm, or perhaps increase my I/O speed?
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int t;
std::cin >> t;
while (t--) {
int n, i;
std::cin >> n;
std::cin.ignore();
std::vector<std::string> accounts(n);
for (i = 0; i < n; ++i) {
std::getline(std::cin, accounts[i]);
}
std::cout << "\n";
std::map<std::string, int> count;
for (i = 0; i < accounts.size(); ++i) {
auto map_it(count.find(accounts[i]));
if (map_it != count.end()) {
map_it->second++;
} else {
count[accounts[i]] = 1;
}
}
for (auto it = count.begin(); it != count.end(); ++it) {
std::cout << it->first << it->second << "\n";
}
if (t != 0) {
std::string blank;
std::getline(std::cin, blank);
}
}
return 0;
}