单词数
Time Limit: 1000msMemory Limit: 32768KB This problem will be judged on HDU. Original ID: 2072
64-bit integer IO format: %I64d Java class name: Main
Prev Submit Status Statistics Next
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
#
Sample Output
4
Source
Basic Problem
巧妙的水题还是stl好用
#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
#include<set>
using namespace std;
int main()
{
char str[1000000 ];
while(gets(str)&&str[0]!='#')
{
set<string> st;
st.clear();
int len=strlen(str);
string S="";
// puts(str);
int ff=1;
for(int i=0;i<=len;i++)
{
if(str[i]>='a'&&str[i]<='z')
{
S+=str[i];
ff=0;
}
else if(!ff)
{
if(!st.count(S)) st.insert(S);
S.clear();
ff=1;
}
}
printf("%d\n",st.size());
}
return 0;
}