hdu 2072 单词数

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

单词数这道题感觉用c写很麻烦,用c++写就比较简单了。不多说,直接贴代码。

#include<iostream>
#include<string>
#include<vector>
#include<sstream>

using namespace std;

int main()
{
    vector<string> s;
    string s0,s1;
    int flag;

    while(getline(cin,s0))
    {
        if(s0=="#")
            break;
        else
        {
            s.clear();
            istringstream sin(s0);
            while(sin>>s1)
            {
                flag=0;
                for(int i=0;i<s.size();i++)
                {
                    if(s[i]==s1)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                    s.push_back(s1);
            }
            cout<<s.size()<<endl;
        }
    }
    return 0;
}

感觉用set更简单,这是网上的代码:

#include <iostream>    
#include <set>    
#include <string>    
#include <sstream>// 不要忘记了   
 
using namespace std;    
  
int main() {    
    string art;    
    while(getline(cin,art) && art != "#"){    
        istringstream stream(art);           
        string word;    
        set<string> map;    
        while(stream >>word){    
            map.insert(word);    
        }    
        cout <<map.size() <<endl;    
    }    
    return 0;    
}  
原文地址:https://www.cnblogs.com/yaoyueduzhen/p/4336104.html