hdoj 2072 单词数

这个题目很有意思,先列出笔者的测试案例,这个测试案例在一行前和一行尾都有空格,特别是行尾的空格,如   holly   shit  不要输出为3个单词,笔者因此WA好几次

   yo u  are  my      friend
   fri end  you are    my friend
you are my friend
   holly   shit  
#  

代码:

用了C++ STL中的Set,算是杀鸡用牛刀了。


#include<iostream>
#include<string>
#include<set>
using namespace std;

int main()
{
    int pos1, pos2;
    set<string> strS;
    string lineStr, str;
    while(getline(cin, lineStr))
    {
        if(!strS.empty())
        {
            strS.clear();
        }
        for(pos2 = pos1 = 0; pos2 < lineStr.length(); ++pos2)
        {
            if(lineStr.at(pos2) == '#')
            {
                return 0;
            }
            else if(lineStr.at(pos2) == ' ')
            {
                str = lineStr.substr(pos1, pos2-pos1);
                pos1 = pos2+1;
                if(!str.empty())
                {
                    strS.insert(str);
                }
            }
        }
        str = lineStr.substr(pos1);
        if(!str.empty())
        {
            strS.insert(str);
        }
        printf("%d\n", strS.size());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值