题目:http://acm.hdu.edu.cn/showproblem.php?pid=2072
说明: 这个题目就是求不同的单词数,其中单词只有小写和空格,以#结束,这种时候采用STL是很好处理的,并且比较容易看懂。
set<string>s;
string st,str;
int ans;
while(getline(cin,str)) //一次读取一行
{
if(str[0] == '#') break;
stringstream ste(str); //变成字符流
while(ste >> st) //一次读取一个单词
{
ans = 0;
s.insert(st);
}
printf("%d\n",s.size());
s.clear();
一般建议不要采用stringstream, 因为这个比较慢。如果数据量比较大的话,就容易TLE。建议采用其他办法。