单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25447 Accepted Submission(s): 5957
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend #
Sample Output
4
string的应用方式,介绍一种新的string头文件函数strtok( char *," ");这个函数是对某个字符取段,第二个参数你可以修改为你所需要的字符即以这个字符为分割字符的标志
代码如下:
代码如下:
#include<iostream>
#include<cmath>
#include<string>
#include<string.h>
#include<map>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
string sp[100],s1;
char s[1000];
char *q;
while(scanf("%[^\n]%*c",s))
{
if(s[0]=='#')
break;
q=strtok(s," ");//取的第一个字符数组段
int count=0;
while(q)
{
s1+=q;//连接成串
for(int i=0;i<count;i++)
if(s1==sp[i])break;
if(i==count)
{
sp[i]=s1;
count++;
}
s1=s1.substr(0,0);
q=strtok(NULL," ");//有兴趣的可以试试将NULL改为别的是什么效果
}
cout<<count<<endl;;
}
return 0;
}