单词数 (STL set集合)
2014-08-09 10:06
281 查看
单词数
Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend #
Sample Output
4 set集合用法及特点: 1,set中的元素从小到大排列且和集合一样没有重复元素; 2,set<string>::iterator s.begin() s.end()#include<iostream> # include<cstring> #include<cstdio> # include<sstream> # include<string> #include<set> using namespace std; string wen,str; set<string> dict; int main() { while(getline(cin,wen)&&wen!="#") { stringstream ch(wen); //在文本串中找单个单词 while(ch >>str) dict.insert(str); printf("%d\n",dict.size()); dict.clear(); } return 0; }
和用map差不多:
#include<iostream> # include<cstdio> # include<string> # include<map> # include<cstring> # include<sstream> using namespace std; map<string ,int> m; string str,ch; /* int main() { while(getline(cin,str)&&str!="#") { int len=str.size(); int i=0; m.clear(); while(i<len) { ch=""; while(i<len&&'a'<=str[i]&&str[i]<='z') ch+=str[i++]; if(ch!="") { m[ch]=1; } i++; } printf("%d\n",m.size()); } return 0; }*/ int main() { while(getline(cin,str)&&str!="#") { stringstream ss(str); m.clear(); while(ss>>ch) { m[ch]=1; } printf("%d\n",m.size()); } return 0; }
相关文章推荐
- 单词数 (STL set集合)
- 单词数 (STL set集合)
- C++ STL入门教程(6)——set(集合)的使用(附完整程序代码)
- 【C++】STL常用容器总结之九:集合set
- STL之set集合容器
- C++ STL 有关于SET集合部分 的自定义数据类型的排序 以及 pair的使用
- L2-005. 集合相似度_STL的 set
- CCCC题集 L2-005. 集合相似度 ( STL之set
- C++中STL容器的分类和基本功能(二):集合(set) 映射(map)
- C++之STL中set的用法 PAT L2-005 集合相似度
- STL之set集合容器
- PTA 7-9 集合相似度(STL之set初体验)
- STL集合容器set按照由小到大或者由大到小的顺序存放元素
- hdu2072单词数(c++ set集合的使用)
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- STL之set集合容器
- C++ STL set集合容器常用用法
- STL 中的集合 set 的用法
- C++ STL 有关于SET集合的基本操作,本文不包含涉及到自定义数据类型的排序
- C++的STL中集合(set)的使用说明