-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastword.cpp
More file actions
74 lines (62 loc) · 1.35 KB
/
Copy pathlastword.cpp
File metadata and controls
74 lines (62 loc) · 1.35 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
// Google Code Jam 2016: Round 1A - The Last Word
string makeUpper(string& s) {
for(size_t i = 0; i < s.length(); i++) {
if (islower(s[i])) {
s[i] = toupper(s[i]);
}
}
return s;
}
bool compareChar(char c1, char c2) {
return c1 > c2;
}
void lastWord(string& s) {
string upper = makeUpper(s);
if (upper.length() == 1) {
cout << upper << endl;
return;
}
char temp = upper[0];
string bigChar = "";
string sub = "";
sub += temp;
for(size_t i = 1; i < upper.length(); i++) {
if (compareChar(upper[i],temp)) {
temp = upper[i];
bigChar += temp;
}
else if (temp == upper[i]) {
bigChar += upper[i];
}
else {
sub += upper[i];
}
}
sort(bigChar.rbegin(), bigChar.rend());
cout << bigChar + sub << endl;
}
string word(string& s) {
for(size_t i = 0; i < s.length(); i++) {
if (islower(s[i])) {
s[i] = toupper(s[i]);
}
}
return s;
}
int main()
{
int i = 1;
int testCases;
string input;
cin >> testCases;
while (i <= testCases && cin >> input) {
cout << "Case #" << i << ": ";
lastWord(input);
i++;
}
return 0;
}