Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
#
Sample Output
4
Solution
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char shuzu[1000];
char shuzu2[50][20];
while(gets(shuzu))
{
if(shuzu[0] == '#')
break;
else if(shuzu == '\0')
printf("0\n");
else
{
int a,b,c,d;
b = 0,c = 0;
for(a = 0;a <= strlen(shuzu);++a)
{
if(shuzu[a] == ' '||shuzu[a] == '\0')
{
shuzu2[b][c] = '\0';
c = 0;
for(d = 0;d < b;++d)
if(strcmp(shuzu2[d],shuzu2[b]) == 0)
break;
if(d == b && shuzu2[b][0] != '\0')
++b;
}
else
{
shuzu2[b][c] = shuzu[a];
++c;
}
}
printf("%d\n",b);
}
}
return 0;
}
注意特殊情况:
1.“ ” 0(测试中任意时刻输入一行空格,结果为0)
2.“ sa zxc ” 2(每个单词前,后,相邻间不止一个空格)
3.“ sa zxc v” 3
4."v sa zxc ” 3(注意首尾可能只是一个字母)
5.任意时刻直接回车,结果为0.