杭电2072

http://acm.hdu.edu.cn/showproblem.php?pid=2072

杭电2072 单词数

 

这题真的教了我不少东西;

一是istringstream函数的使用。

参考:https://zhidao.baidu.com/question/366022043.html
杭电2072 单词数

 

二是提醒我要好好系统复习一遍字符串函数以及输入输出。

 

题目意思很简明,就是给定一组字符串,包含若干的单词,每个单词以空格隔开,问有多少不同的单词。

 

思路

用上述的istringstream函数分割单词后用一个set记录去重即可。

 

AC代码:

#include <iostream>
#include <cstring>
#include <sstream>
#include <set>
using namespace std;
set<string> s;
string a,b,c;
int main(){
    while(getline(cin,a)&& a !="#"){
        istringstream b(a);
        while(b>>c){
            s.insert(c);
        }
        cout<<s.size()<<endl;
        s.clear();
    }
    return 0;
}

 

相关文章:

  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2021-04-30
猜你喜欢
  • 2021-09-25
  • 2022-12-23
  • 2021-06-09
  • 2021-09-27
  • 2021-10-30
  • 2021-08-14
  • 2021-08-31
相关资源
相似解决方案