# Include <bits/stdc ++. h> using namespace STD; int ID [30]; char letter [10]; int main () {// data input char input [1000]; while (scanf ("% s", input) = 1 & input [0]! = '#') {Int n = 0; For (char TMP = 'a'; TMP <= 'Z'; TMP ++) if (strchr (input, TMP )! = NULL) {ID [TMP-'a'] = n ++; letter [ID [TMP-'a'] = TMP;} int Len = strlen (input ), P = 0, q = 0; vector <int> U, V; while (true) {While (P <Len & input [p]! = ':') P ++; If (P = Len) break; while (q <Len & input [Q]! = ';') Q ++; For (INT I = p + 1; I <q; I ++) {u. push_back (ID [input [P-1]-'a']); V. push_back (ID [input [I]-'a']);} p ++; q ++;} // process int P [10], bestp [10], pos [10], ANS = N; For (INT I = 0; I <n; I ++) P [I] = I; do {for (INT I = 0; I <n; I ++) POS [p [I] = I; int WID = 0; For (INT I = 0; I <u. size (); I ++) WID = max (WID, ABS (Pos [U [I]-pos [V [I]); If (WID <ans) {ans = WID; memcpy (bestp, P, sizeof (p) ;}} while (next_permutation (p, p + n); For (INT I = 0; I <n; I ++) cout <letter [bestp [I] <""; cout <"->" <ans <Endl ;} return 0 ;}
Liu rujia code
The summary is to arrange all and then calculate the current minimum bandwidth.
UV 140 bandwidth