STL
1.map[string]统计 , vector<pair<vector<int>, string> >的二级排序
题目:http://codeforces.com/contest/24/problem/B
代码
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>
using namespace std;
struct Node{
int rank[55];
};
int score[100] = { 0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1 };
map<string, Node> mm;
map<string, Node>::iterator ite;
vector<pair<vector<int>, string> > vec1, vec2;
int main(){
// freopen("c:/aaa.txt", "r", stdin);
int T, n, i, j;
string str;
cin>>T;
while(T--){
cin>>n;
for(i=1; i<=n; ++i){
cin>>str;
mm[str].rank[0] += score[i];
mm[str].rank[i]++;
}
}
for(ite=mm.begin(); ite!=mm.end(); ++ite){
vector<int> temp;
for(i=0; i<=50; ++i) temp.push_back((ite->second).rank[i]);
vec1.push_back(make_pair(temp, ite->first) );
swap(temp[0], temp[1]);
vec2.push_back(make_pair(temp, ite->first) );
}
sort(vec1.begin(), vec1.end());
reverse(vec1.begin(), vec1.end() );
cout<<(vec1[0]).second<<endl;
sort(vec2.begin(), vec2.end());
reverse(vec2.begin(), vec2.end() );
cout<<(vec2[0]).second<<endl;
return 0;
}
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>
using namespace std;
struct Node{
int rank[55];
};
int score[100] = { 0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1 };
map<string, Node> mm;
map<string, Node>::iterator ite;
vector<pair<vector<int>, string> > vec1, vec2;
int main(){
// freopen("c:/aaa.txt", "r", stdin);
int T, n, i, j;
string str;
cin>>T;
while(T--){
cin>>n;
for(i=1; i<=n; ++i){
cin>>str;
mm[str].rank[0] += score[i];
mm[str].rank[i]++;
}
}
for(ite=mm.begin(); ite!=mm.end(); ++ite){
vector<int> temp;
for(i=0; i<=50; ++i) temp.push_back((ite->second).rank[i]);
vec1.push_back(make_pair(temp, ite->first) );
swap(temp[0], temp[1]);
vec2.push_back(make_pair(temp, ite->first) );
}
sort(vec1.begin(), vec1.end());
reverse(vec1.begin(), vec1.end() );
cout<<(vec1[0]).second<<endl;
sort(vec2.begin(), vec2.end());
reverse(vec2.begin(), vec2.end() );
cout<<(vec2[0]).second<<endl;
return 0;
}
2.hdoj 1880 魔咒词典
关键词:map[string] ,str.substr(startpos, lenth) ; ite = map.find();
代码写得比较蛋疼。。。
代码
#include <iostream>
#include <string>
#include <map>
#include <iterator>
using namespace std;
map<string, string> m1;
map<string, string> m2;
map<string, string>::iterator ite;
char ch[100];
string str1, str2;
void input(){
while(scanf("%s", ch)){
if(strcmp(ch, "@END@") == 0) break;
str1 = ch;
getchar();
gets(ch);
str2 = ch;
m1[str1] = str2;
m2[str2] = str1;
}
}
void find(){
int n, len;
scanf("%d%*c", &n);
while(n--){
gets(ch);
str1 = ch;
if(ch[0] == '['){
if( (ite = m1.find(str1)) != m1.end() )
printf("%s\n", (ite->second).c_str());
else printf("what?\n");
}else {
if( (ite = m2.find(str1)) != m2.end() ){
len = m2[str1].size();
str2 = (ite->second).substr(1, len-2);
printf("%s\n", str2.c_str());
}else{
printf("what?\n");
}
}
}
}
int main(){
// freopen("c:/aaa.txt", "r", stdin);
input();
find();
return 0;
}
#include <string>
#include <map>
#include <iterator>
using namespace std;
map<string, string> m1;
map<string, string> m2;
map<string, string>::iterator ite;
char ch[100];
string str1, str2;
void input(){
while(scanf("%s", ch)){
if(strcmp(ch, "@END@") == 0) break;
str1 = ch;
getchar();
gets(ch);
str2 = ch;
m1[str1] = str2;
m2[str2] = str1;
}
}
void find(){
int n, len;
scanf("%d%*c", &n);
while(n--){
gets(ch);
str1 = ch;
if(ch[0] == '['){
if( (ite = m1.find(str1)) != m1.end() )
printf("%s\n", (ite->second).c_str());
else printf("what?\n");
}else {
if( (ite = m2.find(str1)) != m2.end() ){
len = m2[str1].size();
str2 = (ite->second).substr(1, len-2);
printf("%s\n", str2.c_str());
}else{
printf("what?\n");
}
}
}
}
int main(){
// freopen("c:/aaa.txt", "r", stdin);
input();
find();
return 0;
}
3.优先队列 priority_queue<MSG, vector<MSG>, greater<MSG> > Q; 用法
hdoj 1509 Windows Message Queue
#include <iostream> #include <queue> #include <algorithm> using namespace std; struct MSG { char name[103]; int par, pri, id; }; bool operator> (MSG m1, MSG m2 ) { if( m1.pri != m2.pri ) return m1.pri > m2.pri; return m1.id > m2.id; } int main() { // freopen( "c:/aaa.txt", "r", stdin ); int cnt; char str1[10]; MSG msg; cnt = 0; priority_queue<MSG, vector<MSG>, greater<MSG> > Q; while( scanf("%s", str1) != EOF ) { ++ cnt; if( str1[0] == 'G' ) { if( Q.empty() ) puts( "EMPTY QUEUE!" ); else { msg = Q.top(); Q.pop(); printf( "%s %d\n", msg.name, msg.par ); } } else { scanf( "%s %d %d", msg.name, &msg.par, &msg.pri ); msg.id = cnt; Q.push( msg ); } } return 0; }
4.map插入复杂结构体,如map<string, Node>
map< string, string > trans_map; typedef map< string, string >::value_type valType; trans_map.insert( valType( "gratz", "grateful" ));
又如:
typedef pair<string, int> PA; typedef map<string, PA>::value_type valType PA p; mp.insert(valType(str1, p));
5.map<string, vector<string> >mp
1022. Digital Library (30)
#include <iostream> #include <sstream> #include <string> #include <map> #include <vector> #include <algorithm> using namespace std; int main() { // freopen("c:/aaa.txt", "r", stdin); map<string, vector<string> >mp[5]; int n; while(scanf("%d", &n) == 1) { getchar(); while(n--) { string id, title, author, keys, publisher, year; getline(cin, id); getline(cin, title); getline(cin, author); getline(cin, keys); getline(cin, publisher); getline(cin, year); mp[0][title].push_back(id); mp[1][author].push_back(id); mp[3][publisher].push_back(id); mp[4][year].push_back(id); istringstream stream(keys); string key; while(stream>>key) { mp[2][key].push_back(id); } } for(int i = 0; i < 5; ++i) { map<string, vector<string> >::iterator ite; for(ite = mp[i].begin(); ite != mp[i].end(); ++ite) { sort(ite->second.begin(), ite->second.end()); } } int m, type; string qstr; scanf("%d", &m); while(m--) { scanf("%d: ", &type); printf("%d: ", type); getline(cin, qstr); cout<<qstr<<endl; vector<string> vec; type--; map<string, vector<string> >::iterator it; it = mp[type].find(qstr); if(it != mp[type].end()) { vector<string> vec = mp[type][qstr]; for(int i = 0; i < vec.size(); ++i) { cout<<vec[i]<<endl; } } else { cout<<"Not Found"<<endl; } } } return 0; }