下面这个例子就是 STL:pair 的用法
#include <iostream> #include <utility> #include <string> using namespace std; int main () { pair <string,double> product1 ("tomatoes",3.25); pair <string,double> product2; pair <string,double> product3; product2.first = "lightbulbs"; // type of first is string product2.second = 0.99; // type of second is double product3 = make_pair ("shoes",20.0); cout << "The price of " << product1.first << " is $" << product1.second << " "; cout << "The price of " << product2.first << " is $" << product2.second << " "; cout << "The price of " << product3.first << " is $" << product3.second << " "; return 0; }
这道题目使用 pair 可以非常方便的解决
Source Code:
1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler 2 #include <stdio.h> 3 #include <iostream> 4 #include <fstream> 5 #include <cstring> 6 #include <cmath> 7 #include <stack> 8 #include <string> 9 #include <map> 10 #include <set> 11 #include <list> 12 #include <queue> 13 #include <vector> 14 #include <algorithm> 15 #define Max(a,b) (((a) > (b)) ? (a) : (b)) 16 #define Min(a,b) (((a) < (b)) ? (a) : (b)) 17 #define Abs(x) (((x) > 0) ? (x) : (-(x))) 18 #define MOD 1000000007 19 #define pi acos(-1.0) 20 21 using namespace std; 22 23 typedef long long ll ; 24 typedef unsigned long long ull ; 25 typedef unsigned int uint ; 26 typedef unsigned char uchar ; 27 28 template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;} 29 template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} 30 31 const double eps = 1e-7 ; 32 const int N = 210 ; 33 const int M = 1100011*2 ; 34 const ll P = 10000000097ll ; 35 const int MAXN = 10900000 ; 36 37 38 map <string,int> mp; 39 map < pair<int,int>,int > G; 40 vector <string> a[60000]; 41 int b[60000]; 42 string str[60000], tmp; 43 44 int main() { 45 int i, j, t, n, m, k, q; 46 scanf("%d",&t); 47 while (t--) { 48 scanf("%d %d %d",&n,&m,&q); 49 mp.clear(); 50 G.clear(); 51 for (i = 0; i < n + m; ++i) { 52 a[i].clear(); 53 cin >> tmp; 54 mp[tmp] = i; 55 cin >> k; 56 for (j = 0; j < k; ++j) { 57 cin >> tmp; 58 a[i].push_back(tmp); 59 } 60 } 61 for (i = 0; i < n + m; ++i) 62 for (j = 0; j < a[i].size(); ++j) 63 G[make_pair (i,mp[a[i][j]])] = 1; 64 while (q--) { 65 cin >> k; 66 for (i = 0; i < k; ++i) { 67 cin >> tmp; 68 str[i] = tmp; 69 b[i] = mp[str[i]]; 70 } 71 int flag; 72 for (i = 0; i < k; ++i) { 73 flag = 1; 74 for (j = 0; j < k; ++j) { 75 if (i != j) { 76 if ( G[ make_pair(b[i],b[j]) ] == 0 || G[ make_pair(b[j],b[i]) ] == 1 ) { 77 flag = 0; 78 break; 79 } 80 } 81 } 82 if (flag) { 83 cout << "1 " << str[i] << endl; 84 break; 85 } 86 } 87 if (!flag) cout << 0 << endl; 88 } 89 cout << endl; 90 } 91 return 0; 92 }