-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
42 lines (38 loc) · 1.04 KB
/
G.cpp
File metadata and controls
42 lines (38 loc) · 1.04 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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
struct Stuff {
char chars;
unsigned int weight;
};
bool comp(Stuff l, Stuff r){
return l.weight > r.weight;
}
int main() {
std::string sinput;
std::vector<Stuff> array;
std::cin >> sinput;
for (int i = 0; i < 26; i++) {
Stuff stuff;
stuff.chars = i + 97;
std::cin >> stuff.weight;
array.push_back(stuff);
sort(array.begin(), array.end(), comp);
}
int shift = 0;
std::string answer = sinput;
for (Stuff stuff: array) {
int counter = count(answer.begin(), answer.end(), stuff.chars);
if (counter >= 2) {
int pos1 = answer.find(stuff.chars);
std::swap(answer[shift], answer[pos1]);
int pos2 = answer.substr(++shift).find(stuff.chars);
if (pos2 + shift != answer.size() - 1) {
std::swap(answer[answer.size() - shift],answer[pos2 + shift]);
}
}
}
std::cout << answer << std::endl;
return 0;
}