博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA10815 - 详解Andy's First Dictionary
阅读量:5132 次
发布时间:2019-06-13

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

Andy's First Dictionary

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.

 

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

 

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

 

Sample Input

Adventures in DisneylandTwo blondes were going to Disneyland when they came to a fork in theroad. The sign read: "Disneyland Left."So they went home.

 

Sample Output

aadventuresblondescamedisneylandforkgoinghomeinleftreadroadsignsothetheytotwowentwerewhen 题目大意:输入一段文本(不超过5000行),按字典序把每个单词排序(需要去重)。
1 #include
2 #include
3 #include
4 5 char s[50005][205]; //数组要开大,否则就runtime error了 6 7 int isLetter(char &ch) 8 { 9 if (ch >= 'a' && ch <= 'z')10 return 1;11 if (ch >= 'A' && ch <= 'Z')12 {13 ch = ch + 'a' - 'A'; //变小写14 return 1;15 }16 return 0; //根据返回的是0还是1判断是不是字母17 }18 19 int cmp(const void * a, const void * b)20 {21 return strcmp((char *) a, (char *) b); //要熟练运用啊(字符串按字典序排序)22 }23 24 int main()25 {26 char c;27 int flag = 0, i = 0, j = 0;28 while (scanf("%c", &c) != EOF) //一个字符一个字符的输入,这是非常简单、有条理、不容易乱的处理方法。之前没用过,要学会使用。29 {30 if (isLetter(c) && !flag) //如果该字符是字母且是第一个字母的话31 {32 j = 0; //当然从j = 0开始记录单词的第一个字母了33 s[i][j++] = c;34 flag = 1;35 }36 else if (isLetter(c) && flag)37 {38 s[i][j++] = c;39 }40 else if (!isLetter(c) && flag) //如果该字符不是字母的话41 {42 s[i][j] = '\0'; //记录该单词(该单词结束),一定不要忘了加'\0'43 i++; //开始准备记录下一个单词44 flag = 0;45 }46 } 47 qsort(s,i,sizeof(s[0]),cmp); //按字典序排序,学会使用qsort,便捷48 for (j = 0; j < i - 1; j++)49 {50 if (strcmp(s[j], s[j + 1]) == 0) //去重操作(因为都排好序了),相同的挨在一起51 continue;52 printf("%s\n", s[j]);53 }54 printf("%s\n", s[j]); //最后一个单独输出55 return 0;56 }

 

 

转载于:https://www.cnblogs.com/youdiankun/p/3302145.html

你可能感兴趣的文章
设计模式之结构型模式
查看>>
poj2569
查看>>
使用pygal_maps_world.i18n中数据画各大洲地图
查看>>
jQuery EasyUI 的下拉选择combobox后台动态赋值
查看>>
timeline时间轴进度“群英荟萃”
查看>>
python if else elif statement
查看>>
网络编程
查看>>
文本隐藏(图片代替文字)
查看>>
java面试题
查看>>
提高码力专题(未完待续)
查看>>
pair的例子
查看>>
前端框架性能对比
查看>>
uva 387 A Puzzling Problem (回溯)
查看>>
12.2日常
查看>>
同步代码时忽略maven项目 target目录
查看>>
Oracle中包的创建
查看>>
团队开发之个人博客八(4月27)
查看>>
发布功能完成
查看>>
【原】小程序常见问题整理
查看>>
C# ITextSharp pdf 自动打印
查看>>