uva 156 - 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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?