/
3173.cpp
66 lines (57 loc) · 1.14 KB
/
3173.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
/*
Problem: 3173 - Wordfish
Author: Andrés Mejía-Posada
*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
int dist(string s){
int ans = INT_MAX;
int n = s.size();
for (int i=0; i<n-1; ++i) ans = min(ans, abs(s[i] - s[i+1]));
return ans;
}
int main(){
string s;
while (cin >> s){
assert(s.size() > 1);
deque<string> p(1, s);
string t;
t = s;
for (int i=0; i<10; ++i){
next_permutation(t.begin(), t.end());
p.push_back(t);
}
t = s;
for (int i=0; i<10; ++i){
prev_permutation(t.begin(), t.end());
p.push_front(t);
}
sort(p.begin(), p.end()); //Useless?
int score = -1; string ans;
for (deque<string>::iterator k = p.begin(); k != p.end(); ++k){
int t = dist(*k);
if (t > score){
score = t;
ans = *k;
}
}
cout << ans << score << endl;
}
return 0;
}