hdu 2072 单词数

此题困扰我良久,半年前做题时不会用C++STL,学长A说用set,学长B说用字典树。好吓人的说,但既然分类是字符串处理,肯定有好想但实现不太容易的办法。

半年后再看,用vector<string>和find()或count()就OK了,思路对了用C语言也能做,就是没有现成的函数可以调用,要自己处理。

先贴代码,有时间整理一下半年前的思路。

 1 # include<iostream>
 2 using namespace std;
 3 # include<vector>
 4 # include<algorithm>
 5 vector <string> ans;
 6 string mysub(string &s)
 7 {
 8     string s0;
 9     int l=0;
10     while(s[l]==' ') s.erase(l,1);
11     //cout<<s<<endl;
12     while(s[l]!=' ') l++;
13     //cout<<s<<endl;
14     s0=s.substr(0,l);
15     s.erase(0,l);
16     //cout<<s0<<endl;
17     //cout<<s<<endl;
18     return s0;
19 }
20 bool myempty(string s)
21 {
22     int i;
23     for(i=0;i<s.size();i++)
24         if(s[i]!=' ') break;
25     return i>=s.size();
26 }
27 int main()
28 {
29     string s,t;
30     while(getline(cin,s)&&s[0]!='#')
31     {
32         while(!s.empty()&&!myempty(s))
33         {
34             t=mysub(s);
35             if(count(ans.begin(),ans.end(),t)==0) ans.push_back(t);
36         }
37         cout<<ans.size()<<endl;
38         ans.clear();
39     }
40     return 0;
41 }
铁牌狗敬上

 

posted @ 2014-05-20 11:28  So_Young  阅读(332)  评论(0编辑  收藏  举报