-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem62.cpp
More file actions
50 lines (42 loc) · 1.15 KB
/
problem62.cpp
File metadata and controls
50 lines (42 loc) · 1.15 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class WordNiceness {
public:
int n;
vector<string> words;
void readInput() {
cout << "Enter number of words: ";
cin >> n;
words.resize(n);
cout << "Enter words: " << endl;
for (int i = 0; i < n; ++i)
cin >> words[i];
}
vector<int> computeNiceness() {
vector<string> inserted; // sorted order
vector<int> result;
for (const string &word : words) {
// Find how many inserted words are less than word
auto pos = lower_bound(inserted.begin(), inserted.end(), word);
result.push_back(pos - inserted.begin());
// Insert word keeping sorted order
inserted.insert(pos, word);
}
return result;
}
void print(const vector<int> &niceness) {
cout << "Niceness values:" << endl;
for (int v : niceness)
cout << v << endl;
}
};
int main() {
WordNiceness wn;
wn.readInput();
vector<int> niceness = wn.computeNiceness();
wn.print(niceness);
return 0;
}