use stringstream

Time Limit:3000MS     Memory Limit:0KB

Description

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 Disneyland

Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left."

So they went home.

Sample Output

a

adventures

blondes

came

disneyland

fork

going

home

in

left

read

road

sign

so

the

they

to

two

went

were

when

题解:

这道题最重要的就是stringstream的使用。

  1. 使用getline(cin,string)函数整行读取。之所以不直接读取,是因为两个单词间有标点符号时,会误读为一个单词,还是需要后续的处理。

  2. 由于最后要求输出的是小写,也方便排序,那么每输入一行就进行标点变为空格,大写变为小写的变换。处理完之后,就需要用到stringstream(字符串流)类,头文件是<sstream>,例如stringstream ss,ss.clear()是将字符串流清空,ss.str(string)是将ss读入对象初始化为形参 string,那么执行ss>>str,那么执行 while(ss>>str[k])k++;就可以将不带空格的字符串一一读入,从而实现了单词之间的分离。

  3. ​排序之后,使用pre记录刚刚输出的字符串,如果当前字符串与pre相同,则是重复的,不输出。因为排序后相同的字符串是连在一块的,所以用这种方法就将重复的全部排除了。

以下是代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <sstream>
using namespace std;
string str[100000],s;
stringstream ss;
int k=0;
int main(){
//freopen("1.in","r",stdin);
while(getline(cin,s)){
int len = s.length();
for(int i=0;i<len;i++){//将标点变为空格,大写变为小写。
char ch = s.at(i);
if(islower(ch))continue;
if(isupper(ch))s.at(i)-='A'-'a';
else s.at(i)=' ';
}
ss.clear();
ss.str(s);
while(ss>>str[k])k++;
}
sort(str,str+k);//排序
string pre="";
for(int i=0;i<k;i++){
if(str[i]==pre)continue;//重复的不输出
cout << str[i]<<endl;
pre =str[i];
}
}

Winter-2-STL-E Andy's First Dictionary 解题报告及测试数据的更多相关文章

  1. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  2. 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...

  3. LeetCode 953 Verifying an Alien Dictionary 解题报告

    题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...

  4. 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. UVa 10815 Andy's First Dictionary

    感觉这道题要比之前几个字符串处理的题目难度要大了一些. 题目大意:给若干行字符串,提取出所有单词并去掉重复的,最后按字典顺序输出. 对于输入大致有两种思路,一种是逐个读入字符,遇到字母的话就放到wor ...

  6. UVa10815.Andy's First Dictionary

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. Problem C: Andy's First Dictionary

    Problem C: Andy’s First DictionaryTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 5[Submit] ...

  8. 【UVA - 10815】Andy's First Dictionary (set)

    Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英 ...

  9. UVA-10815 Andy's First Dictionary (非原创)

    10815 - Andy's First Dictionary Time limit: 3.000 seconds Problem B: Andy's First DictionaryTime lim ...

随机推荐

  1. 【cb2】扩展硬盘

    1.硬盘为sata串口 2.参考 http://docs.cubieboard.org/tutorials/ct1/installation/moving_rootfs_from_nandflash_ ...

  2. ChemDraw怎么绘制H-点或H-划

    ChemDraw软件是一款全球领先的化学绘图工具,能够绘制各类化学结构图形和化学方程式,在基础化学.有机化学和分析化学等领域得到了广泛的应用.H-点和H-划是日常作图过程中使用频率较高的化学符号,必须 ...

  3. VC++:ActiveX Test Container

    VC++6.0安装后包含了ActiveX Test Container工具,位置为: "C:\Program Files (x86)\Microsoft Visual Studio\Comm ...

  4. 使用ProcDump工具抓取dump

    首先得到要抓取的进程号 cd %windir%\syswow64\inetsrvappcmd list wp得到pid之后, 在任务管理器里发现w3wp.exe的CPU总在49%-60%左右, 间歇性 ...

  5. iOS开发之--如何修改TabBarItem的title的字体和颜色/BarButtonItem的title的字体大小和颜色/添加背景图片,并添加点击方法

    在进行项目的过程中,我们往往会遇到各种各样的自定义颜色和字体,下面提供一种修改系统自带的TabBarItem的字体和颜色的方法,希望能帮到大家: [[UITabBarItem appearance] ...

  6. aar

    aar是一个类似于jar的文件格式.但是他们之间是有区别的.jar:仅仅包含class和清单文件,没有资源文件.aar:包含了class文件和资源文件.说白了就是Android的专属“jar” 将代码 ...

  7. LAMP集群项目五 项目备份

    1.打包到本地 2.推送到备份服务器 3.删除若干天前的备份 ip=`awk '/IPADDR/' /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F ...

  8. LAMP集群项目三 配置业务服务器

    安装MySQL 参考脚本:CentOS6.5一键安装MySQL5.5.32(源码编译) 在备份服务器上配置rsync推送任务 在备份服务器上配置  /etc/rsyncd.conf #在所有的客户端都 ...

  9. js的等于号==的判断

    var str=0; str == "" 将返回true:

  10. java基础之Flex弹性布局、JSP错误处理以及Log4J

    一.Flex弹性布局 1.产生的比较晚,目前在移动网页开发中可以使用,而且逐渐成为主流. 在桌面网页开发中使用的比较少(主要是桌面浏览器的兼容性问题更加严重) 2.开启方法: 在容器标签上加上 dis ...