-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProblemC.cpp
78 lines (73 loc) · 1.85 KB
/
ProblemC.cpp
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
75
76
77
78
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int steps;
cin >> steps;
char max_cha = 'f';
for(int i = 0 ; i < s.size() ; i ++) {
if(s[i] < max_cha) {
max_cha = s[i];
// pos_max = i;
}
}
//cout << max_cha << " Max cha" <<endl;
// save positions
vector<int> pos_maxes;
for(int i = 0 ; i < s.size() ; i++) {
if(s[i] == max_cha) {
pos_maxes.push_back(i);
}
}
int _max = INT_MAX;
int pos_max = 0;
char huong;
for(auto pos : pos_maxes) {
if(pos == 0) {
// handle right
if(_max > s[pos+1] + s[pos]) {
pos_max = pos;
_max = s[pos+1] + s[pos];
huong='r';
}
}else if (pos == s.size() -1 ) {
if(_max > s[pos-1] + s[pos]) {
pos_max = pos;
_max = s[pos-1] + s[pos];
huong='l';
}
} else {
// left right
if(_max > s[pos+1] + s[pos]) {
pos_max = pos;
_max = s[pos+1] + s[pos];
huong='r';
}
if(_max > s[pos-1] + s[pos]) {
pos_max = pos;
_max = s[pos-1] + s[pos];
huong='l';
}
}
}
if(huong == 'l') {
// handle output
for(int i = 0;i<steps/2; i ++) {
cout << s[pos_max] << s[pos_max-1];
}
if(steps%2==1) {
cout << s[pos_max];
}
//cout << endl;
}else {
for(int i = 0;i<steps/2; i ++) {
cout << s[pos_max] << s[pos_max+1];
}
if(steps%2==1) {
cout << s[pos_max];
}
//cout << endl;
}
//cout << max_cha << " " << endl;
}