结构体代码实现以及相关例题
发布日期:2021-09-29 21:09:51 浏览次数:2 分类:技术文章

本文共 3542 字,大约阅读时间需要 11 分钟。

起初是不太了解结构体,结构体数组对变量的引用,

D - 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
#include
#include
#include
#include
using namespace std;const int num = 100;const int len = 30;struct node1 { char s1[len]; char s2[len]; bool flag;} a[num]; //定义一个结构体bool cmp1(node1 a,node1 b) { return strcmp(a.s2,b.s2) < 0;}bool cmp2(node1 a,node1 b) { return strcmp(a.s1,b.s1) < 0;}int main() { char tmp[len]; int t = 0; int l; memset(a,0,sizeof(a)); /* 大写字母转换为小写 排序 为结构体赋值 */ while ((scanf("%s",tmp)!=EOF) && tmp[0] != '#') {//开始一直疑惑怎么判断多个单词输入用字符串还是char型 //在未知单词个数下如何判断最后的字符'#' strcpy(a[t].s1,tmp); l = strlen(tmp);//每个字符串的长度 //printf("%d",l); for (int i = 0; i < l; i++) if (isupper(tmp[i]))//判断字符c是否为大写英文字母 tmp[i] = tolower(tmp[i]);//将给定的大写字母转化为小写字母--->没有必要进行32运算 sort(tmp,tmp+l); //*****/ strcpy(a[t].s2,tmp);//将从新转换好的复制给s2 t++; } sort(a,a+t,cmp1); if (strcmp(a[0].s2,a[1].s2)) a[0].flag = true; for (int i = 1; i < t-1; i++) if (strcmp(a[i].s2,a[i-1].s2) && strcmp(a[i].s2,a[i+1].s2)) a[i].flag = true;//比较大小 if (strcmp(a[t-1].s2,a[t-2].s2)) a[t-1].flag = true;// sort(a,a+t,cmp2); for (int i = 0; i < t; i++) { if (a[i].flag) cout<
<
代码分析

isupper函数:判断字符是否为大写英文字符

tolower函数:将给定的大写字母转小写

单词的获取可以用char型数组逐个获取,直到a[0]==‘#’

 

转载地址:https://blog.csdn.net/hou_shiyu/article/details/81195444 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:入栈出栈代码实现以及相关例题
下一篇:优先队列代码实现以及相关例题

发表评论

最新留言

很好
[***.229.124.182]2024年03月18日 04时53分33秒