题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072
思路简单,但是注意一个细节就是最后可能是空格结束的,就是这儿让我WA啦好多次呀。。。
code:
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<string> using namespace std; int main() { string s1,a; int i; while(getline(cin,s1)) { if(s1=="#") break; int s=0; set<string> w; set<string>::iterator w1; for(i=0;i<s1.size();i++) { if(s1[i]==' ') { if(i-s!=0) { a=s1.substr(s,i-s); //cout<<"AAA "<<i<<" "<<a<<endl; s=i+1; w.insert(a); } else { s=i+1; } } } a=s1.substr(s,s1.size()); //cout<<a<<endl; w.insert(a); //for(w1=w.begin();w1!=w.end();w1++) //cout<<*w1<<endl; if(s1[s1.size()-1]==' ') //判断最后是否为空格 cout<<w.size()-1<<endl; else cout<<w.size()<<endl; } return 0; }
原文地址:http://blog.csdn.net/u010304217/article/details/39482423