Description
XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语。于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来。
现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词。
要求:如果文章中有相同的单词,那么仅仅输出一次;而且如果两个单词只有大小写不同,将他们视为相同的单词。
Input
测试数据将输入一篇文章。不超过5000行,每一行最多200个字符,并以EOF结束。
Output
按照字典序输出他学到的单词,每行输出一个单词,输出单词时所有的字母全部小写。
数据保证最多有5000个需要输出的单词。
Sample Input
样例输入①
a a a a a a a a, a a a a a a.
a a a a b a a a. a? a!!!
样例输入②
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
b
样例输出②
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
Hint
输入可能包含标点符号,但标点符号显然不能算作单词的一部分。
昨天学习了STL的一些东西,熟练起来的话做题真的是很简单,这道题是让统计单词的种类,以及按字典序输出,这时候就可以用map,map能够去重,并且自动排序(升序排列) 当然也用了string,这可以说是我首次用string,以前只是知道它,但是没有在做题当中用过,这次学到了好多 string中有好多友好的函数,使用起来能够省去很多麻烦,并且它能够直接相加(+)和赋值(=)
在这道题中需要判断是不是字母,string的 isalpha(c)就能够直接判断 c 是不是字母,是则返回true,否则返回false
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <cstring>
using namespace std;
int main()
{
string s;
set<string> se;
while(cin>>s)
{
string c="";
for(int i=0;i<s.size();i++)
{
if(isalpha(s[i]))//判断s[i]是不是字母,是的话返回true,否则返回false
c+=tolower(s[i]);
else if(c!="")//c为空的时候不能够加入集合,不然会PE
{
se.insert(c);
c="";//将c加入集合后清空c
}
}
if(c!="")
se.insert(c);
}
for(set<string>::iterator ite=se.begin();ite!=se.end();ite++)//用迭代器输出
cout<<*ite<<endl;
return 0;
}
本来以为是很复杂的问题,没想到用STL三十行代码就给解决了