HDU 2072 单词数 set使用

单词数
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42569    Accepted Submission(s): 10314


Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。


Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。


Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。


Sample Input
you are my friend
#


Sample Output
4


Author
Lily


Source
浙江工业大学网络选拔赛


Recommend
linle   |   We have carefully selected several similar problems for you:  2074 2052 1251 2024 2058 

来源: http://acm.hdu.edu.cn/showproblem.php?pid=2072

#include <iostream>  //不知道string输出标识符 只好流输出
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <stdlib.h>
using namespace std;
char A[100000];
char B[1000];
int Cal(void)
{
    if(A[0]=='#') exit(0);
    set<string> S;
    int len=strlen(A);
    for(int i=0,count=0;i<=len;i++)//到len结束以便于处理最后一个单词
    {
        if(A[i]!=' '&&i!=len) B[count++]=A[i];
        else
        {
            if(count==0) continue;//防止多个空格的干扰   WA了一次
            B[count]=0;
            S.insert(B);
            count=0;
        }
    }
    set<string>::iterator it;
 //for(it = S.begin();it != S.end();it++)   //没注释掉结果WA了一次
 //   cout << *it <<endl;
    return S.size();
}
int main(void)
{
    while(gets(A)!=NULL) printf("%d\n",Cal());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值