-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInversMax.cpp
More file actions
61 lines (54 loc) · 1.55 KB
/
Copy pathInversMax.cpp
File metadata and controls
61 lines (54 loc) · 1.55 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
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 0x3f3f3f3f;
string buildWord(const string &str, int l, int r) {
string outputString;
for (size_t i = l; i <= r; i++)
outputString += str[i];
return outputString;
}
bool isWord(const string &str) {
for (auto chr : str)
if ((chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z'))
return true;
return false;
}
string s;
string sol;
void solve() {
int maximumStringSize = -INF;
for (size_t i = 0; i < s.size(); i++)
if (s[i] != ' ') {
size_t pos = s.find(' ', i);
string word;
if (pos != string::npos)
word = buildWord(s, i, pos - 1);
else
word = buildWord(s, i, s.size() - 1);
if (isWord(word))
maximumStringSize = max(maximumStringSize, (int)word.size());
i = pos - 1;
}
for (size_t i = 0; i < s.size(); i++)
if (s[i] != ' ') {
size_t pos = s.find(' ', i);
string word;
if (pos != string::npos)
word = buildWord(s, i, pos - 1);
else
word = buildWord(s, i, s.size() - 1);
if (word.size() == maximumStringSize && isWord(word))
reverse(word.begin(), word.end());
sol += word;
i = pos - 1;
} else
sol += s[i];
cout << sol << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
getline(cin, s);
solve();
return 0;
}