forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3359 - Wordfish.cpp
executable file
·56 lines (43 loc) · 977 Bytes
/
3359 - Wordfish.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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int f(string s){
int aux=INT_MAX;
for(int i=0;i+1<s.size();i++) aux=min(aux,abs(s[i+1]-s[i]));
return aux;
}
struct pass{
string s;
int d;
pass(){
}
pass(string _s){
s=_s;
d=f(s);
}
bool operator < (pass X) const{
if(d!=X.d) return d>X.d;
return s<X.s;
}
};
int main(){
string s,aux;
pass L[21];
while(cin>>s){
L[0]=pass(s);
aux=s;
for(int i=1;i<11;i++){
prev_permutation(aux.begin(),aux.end());
L[i]=pass(aux);
}
aux=s;
for(int i=11;i<21;i++){
next_permutation(aux.begin(),aux.end());
L[i]=pass(aux);
}
sort(L,L+21);
cout<<L[0].s<<L[0].d<<endl;
}
return 0;
}