HDU 1004

1 #include <iostream>
2 #include <string>
3 #include <vector>
4  using namespace std;
5  struct v{
6  string s;
7  int value;
8 };
9
10 vector<v> vv;
11 vector<v> ::iterator it;
12
13 int main(){
14 int n;
15 string s;
16 string r;
17 int i;
18 int max=0;
19 int find=0;
20
21 while(cin>>n,n){
22 vv.clear();
23
24 for(i=0;i<n;i++){
25 cin>>s;
26 find=1;
27
28 for(it=vv.begin();it!=vv.end();it++){
29 if((*it).s.compare(s)==0){
30 (*it).value++;
31 find=0;
32 }
33 }
34
35 if(find){
36 v vt;
37 vt.s=s;
38 vt.value=1;
39 vv.push_back(vt);
40 }
41 }
42
43
44 int max=0;
45 for(i=0;i<vv.size();i++){
46
47 if(vv[i].value>max){
48 max=vv[i].value;
49 r=vv[i].s;
50 }
51 }
52
53
54 cout<<r<<endl;
55
56 }
57
58
59
60 return 0;
61 }

一开始用MAP做的、虽然没做出来 但是也学到了不少知识 贴出来有空研究

#pragma warning (disable : 4786)

#include
<iostream>
#include
<map>
#include
<string>
using namespace std;
struct v{
string value;
int sort;
v():value(
""),sort(0){}
//,sort(0)
bool operator < (const v &a) const
{
return a.sort>sort;
}
};
/*
如果不是结构体
struct cmps{


bool operator ()(const v &a ,const v &b){
return a.sort>b.sort;
}
};
*/
int main(){

//map<v,int> m; //排序用KEY排序
map<v,int>::iterator it;
int n;
int max;
string s="";
string r="";

while(cin>>n,n){
map
<v,int> m; //排序用KEY排序
int count=0;
max
=0;
r
="";
while(n--){
count
++;
cin
>>s;
v vv;
vv.value
=s;
if(vv.sort==0)
vv.sort
=count;
m[vv]
++;
}


for(it=m.begin();it!=m.end();it++){

if((*it).second>max){
max
=(*it).second;
r
=(string)(*it).first.value;


}

}

cout
<<r<<endl;

}
return 0;
}
原文地址:https://www.cnblogs.com/lfzark/p/2072171.html