// @BEGIN_OF_SOURCE_CODE /* @JUDGE_ID: 17243NT 156 C++ "The immortal selection sort" */ // Send to judge@uva.es #include #include #include #ifdef ONLINE_JUDGE #define ins cin #define outs cout #else #define ins fin #define outs fout ifstream fin("myprog.in"); ofstream fout("myprog.out"); #endif typedef struct { char w[25]; char f[26]; } word; word dict[1000]; int nwords; bool anagrams(const word &a, const word &b); int main() { char tword[25]; char anana[1000][25]; int i, j, l; while(ins >> tword) { if(strcmp(tword, "#") == 0) break; strcpy(dict[nwords].w, tword); for(i = 0; i < 52; i++) dict[nwords].f[i] = 0; l = strlen(tword); for(i = 0; i < l; i++) { if('A' <= tword[i] && tword[i] <= 'Z') dict[nwords].f[tword[i] - 'A']++; else dict[nwords].f[tword[i] - 'a']++; } nwords++; } l = 0; for(i = 0; i < nwords; i++) { for(j = 0; j < nwords; j++) { if(i == j) continue; if(anagrams(dict[i], dict[j])) break; } if(j == nwords) strcpy(anana[l++], dict[i].w); } // The immortal selection sort for(i = 0; i < l; i++) { int min = i; for(j = i + 1; j < l; j++) { if(strcmp(anana[min], anana[j]) > 0) min = j; } strcpy(tword, anana[min]); strcpy(anana[min], anana[i]); strcpy(anana[i], tword); cout << anana[i] << endl; } return 0; } bool anagrams(const word &a, const word &b) { for(int i = 0; i < 26; i++) if(a.f[i] != b.f[i]) return false; return true; } // @END_OF_SOURCE_CODE