uva 156 - Ananagrams(排序水题)
Ananagrams |
Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.
Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be ``rearranged'' at all. The dictionary will contain no more than 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus tIeD and EdiT are anagrams. The file will be terminated by a line consisting of a single #.
Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.
Sample input
ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries #
Sample output
Disk NotE derail drIed eye ladder soon
#include<stdio.h> #include<string.h> #include<ctype.h> #define MAXN 1004 char list[MAXN][24]; int rank[MAXN]; typedef struct word{ char detail[24]; int num; }word; word words[MAXN]; int swapword(int n) {// 对转换成lowcase的所有单词进行排序 int i, j, flag, k; char temp[24]; for(i=0; i<n-1; ++i) { flag = 1; for(j=0; j<n-1-i; ++j) { if(strcmp(words[j].detail, words[j+1].detail) > 0) { strcpy(temp, words[j].detail); strcpy(words[j].detail, words[j+1].detail); strcpy(words[j+1].detail, temp); k = words[j].num, words[j].num = words[j+1].num, words[j+1].num = k; flag = 0; } } if(flag) break; } } int ranklist(int n) {//查找出现一次的单词,并将初始下标记录 int i, j, k, count = 0, t; for(i=0; i<n; i++) { t = 0; while(i+t+1 < n && strcmp(words[i+t].detail, words[i+t+1].detail) == 0) t++; if(t == 0) rank[count++] = words[i].num; else if(i+t+1 >= n) break; else i = i+t; } return count; } int main() { int i, j, k, count=0, n, len, flag, t, m; char temp[24], ch; while(scanf("%s", temp) != EOF) { if(strcmp(temp, "#") == 0) break; else strcpy(list[count++], temp); } for(i=0; i<count; ++i) { strcpy(temp, list[i]); len = strlen(temp); // 将单词转成lowcase for(j=0; j<len; ++j) if(isupper(temp[j])) temp[j] = tolower(temp[j]); for(j=0; j<len-1; ++j) {//将单词中的字母进行排序 flag = 1; for(t=0; t<len-1-j; ++t) { if(temp[t] > temp[t+1]) { flag = 0; ch = temp[t], temp[t] = temp[t+1], temp[t+1] = ch; } } if(flag) break; } strcpy(words[i].detail, temp); words[i].num = i; } swapword(count); t = ranklist(count); for(i=0; i<t-1; ++i) {//输出前按字典顺序排序 flag = 1; for(j=0; j<t-1-i; ++j) { if(strcmp(list[rank[j]], list[rank[j+1]]) > 0) { k = rank[j]; rank[j] = rank[j+1]; rank[j+1] = k; flag = 0; } } if(flag) break; } for(i=0; i<t; ++i) printf("%s\n", list[rank[i]]); return 0; }
解题思路:
几次排序
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)