题目链接
题目:
测试数据将输入一篇文章。不超过5000行,每一行最多200个字符,并以EOF结束。
全部转换为小写 字典序输出 其中所有出现过的单词 (重复的只需要输出一次)
样例输入①
a a a a a a a a, a a a a a a. a
a a a b a a a. a? a!!!
样例输出①
a b
样例输入②
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.
样例输出②
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
分析:
本想着 用 cin 每次输入一个子字符串, 对每一个输入的子字符串处理,输入的空格就主动过滤了
(cin 并不能把空格作为字符输入)
大写变小写,遇到标点删除,处理过后,把这个子字符串,加入到 set<string> 中。
这是错误的 。。。
BUG:对于 A!!a 这个子串,用这种方法处理后得到的是一个单词 aa 。
但正确的结果应该是两个单词 a 。。。。。
修改
用 ( c = getchar() ) != eof 来对每一个字符处理 。
1.。。如果遇到 字母 就存入 临时 str 中,
2.。。如果遇到空格就把之前储存的临时 str
insert到 set 中,临时 str 清空。
3.。。如果遇到 标点 ,就把标点 变成 空格,这样就能把标点前后的字母区分开了 (这个判断要写到最前面)
#include<iostream>
#include<string>
#include<set>
#include <cstdio>
using namespace std;
set<string> s;
int main()
{
string str = "";
char t;
while(scanf("%c",&t) != EOF)
{
if(t < 65 || (t > 90 && t < 97) || t >122)
{
t = 32;
}
if(t == ' ')
{
s.insert(str);
str = "";
continue;
}
else if(t <= 90)
{
t = t + 32;
}
str += t;
}
set<string>::iterator it;
for(it = ++s.begin();it != s.end(); it++)
{
string t1 = *it;
cout << t1 << endl;
}
return 0;
}