Write a program that, given a sequence of words, computes how many characters have in total the words that appear once, how many characters have in total the words that appear twice, how many characters have in total the words that appear thrice, etc.
Input
Input consists of several cases. Each case begins with a number 1 ≤ n ≤ 10^5, followed by n words.
Output
For each case of the input and for each number x of repetitions, print in a line the total number of characters (not counting repetitions) of the words that appear exactly x times, If there is no word with x repetitions, do not print anything for this x. Print an empty line after every case.
Public inputs
Input
12 z z ab dddddd z z ab yy yy yy yy x
2 c3po r2d2
Output
1 : 7
2 : 2
4 : 3
1 : 8
The program must have the following interface:
1 2 3 4 5 6 7
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
//code here
}
|
The problem would be easy if you were allowed to change whatever part of the program but since you can only write in the main this discards efficient stuff like sorts, functions, etc.
I have tried during 6 straight hours and I have not been able to get a program efficient enough.
The best one I got so far I believe it's this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
int main() {
int size;
while (cin >> size) {
vector <string> v (size);
vector <string> result;
for (int i = 0; i < size; ++i) {
cin >> v[i];
if (not result.empty()) {
bool repeated = false;
for (int j = 0; j < result.size() and not repeated; ++j) {
if (v[i] == result[j]) repeated = true;
}
if (not repeated) result.push_back(v[i]);
}
else result.push_back(v[0]);
}
vector <int> counter (result.size(), 0);
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < v.size(); ++j) {
if (result[i] == v[j]) ++counter[i];
}
}
for (int condition = 1; condition < result.size(); ++condition) {
int sum = 0;
for (int i = 0; i < result.size(); ++i) {
if (counter[i] == condition) {
sum += result[i].size();
}
}
if (sum != 0) cout << condition << " : " << sum << endl;
}
cout << endl;
}
}
|
Here the code explained in parts:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
vector <string> v (size);
vector <string> result;
for (int i = 0; i < size; ++i) {
cin >> v[i];
if (not result.empty()) {
bool repeated = false;
for (int j = 0; j < result.size() and not repeated; ++j) {
if (v[i] == result[j]) repeated = true;
}
if (not repeated) result.push_back(v[i]);
}
else result.push_back(v[0]);
}
|
Reads input and saves it in a vector called v and simultaneously writes all different strings in a vector called result. This means result doesn't have repeated strings.
1 2 3 4 5 6
|
vector <int> counter (result.size(), 0);
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < v.size(); ++j) {
if (result[i] == v[j]) ++counter[i];
}
}
|
Creates a vector called counter which will hold the number of times a repeated string appears.
1 2 3 4 5 6 7 8 9 10
|
for (int condition = 1; condition < result.size(); ++condition) {
int sum = 0;
for (int i = 0; i < result.size(); ++i) {
if (counter[i] == condition) {
sum += result[i].size();
}
}
if (sum != 0) cout << condition << " : " << sum << endl;
}
cout << endl;
|
A loop that will iterate result.size() times since we know result hold all different strings. A variable called sum will be added the size of the string depending if the word is repeated condition times.