码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2072 单词数

时间:2015-03-13 23:33:21      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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;    
}  

 

hdu 2072 单词数

标签:

原文地址:http://www.cnblogs.com/yaoyueduzhen/p/4336104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!