集合set 和 字符串处理-UVaOJ-10815

3 篇文章 0 订阅

题目链接

题目:

测试数据将输入一篇文章。不超过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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值