hdoj-2072-單詞數

比較簡單的一道題,主要是對字符串的一些基本的操作,這裏用到了string.h裏的strtok函數來對字符串進行分割,然後用STL的set存儲每個單詞,最後set的大小即爲不同的單詞的個數。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<set>
int main()
{
	char buf[1024];
	std::set<std::string> words;
	while(gets(buf) != NULL)
	{
		if(buf[0] == '#')
			break;
		words.clear();
		char *tmp = strtok(buf," ");
		while(tmp != NULL)
		{
			words.insert(tmp);
			tmp = strtok(NULL," ");
		}
		printf("%d\n",words.size());
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章