您的位置:首页 > 其它

hdu 2072 单词数

2015-03-13 21:57 204 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: