/* File: 140.cpp Author: ACboy Date: 2010-3-25 Result: 1A Description: UVa 140 - Bandwidth */ #include <iostream> #include <algorithm> using namespace std; int data[80][80]; int vis[30]; int getMax(int pos, char * p) { int i; int res = 0; int temp = 1; for (i = pos - 1; i >= 0; i--, temp++) { if (data[p[i] - 'A'][p[pos] - 'A']) { if (temp > res) res = temp; } } temp = 1; int len = strlen(p); for (i = pos + 1; i < len; i++, temp++) { if (data[p[i] - 'A'][p[pos] - 'A']) { if (temp > res) res = temp; } } return res; } int main() { #ifndef ONLINE_JUDGE freopen("140.txt", "r", stdin); #endif char input[100]; while (gets(input)) { if (strcmp(input, "#") == 0) break; memset(data, 0, sizeof(data)); int len = strlen(input); int i, j; char begin; memset(vis, 0, sizeof(vis)); for (i = 0; i < len; i++) { if (input[i] == ':' || input[i] == ';') continue; else if (input[i + 1] == ':') { begin = input[i]; } else { data[begin - 'A'][input[i] - 'A'] = 1; data[input[i] - 'A'][begin - 'A'] = 1; } if (isalpha(input[i]) && !vis[input[i] - 'A']) { vis[input[i] - 'A'] = 1; } } char d[30]; int count = 0; for (i = 0; i < 26; i++) if (vis[i]) { d[count++] = i + 'A'; } d[count] = '/0'; int ans = 1 << 10; char ansStr[30]; do { int max = -1; int temp; for (i = 0; i < count; i++) { temp = getMax(i, d); if (temp > max) max = temp; } if (max < ans) { strcpy(ansStr, d); ans = max; } else if (ans == max && strcmp(ansStr, d) > 0) { strcpy(ansStr, d); } } while (next_permutation(d, d + count)); for (j = 0; j < count; j++) { cout << ansStr[j] << " "; } cout << "-> " << ans << endl; } return 0; }