单词数
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());
}