首页 > 代码库 > UVa 1592 数据库(c++pair)

UVa 1592 数据库(c++pair)

Input 

Input contains several datasets. The first line of each dataset contains two integer numbersn and m (1技术分享n技术分享10000, 1技术分享m技术分享10), the number of rows and columns in the table. The following n lines contain table rows. Each row hasm column values separated by commas. Column values consist of ASCII characters from space (ASCII code 32) to tilde (ASCII code 126) with the exception of comma (ASCII code 44). Values are not empty and have no leading and trailing spaces. Each row has at most 80 characters (including separating commas).

Output 

For each dataset, if the table is in PNF write to the output file a single word ``YES" (without quotes). If the table is not in PNF, then write three lines. On the first line write a single word ``NO" (without quotes). On the second line write two integer row numbers r1 andr2 (1技术分享r1,r2技术分享n,r1技术分享r2), on the third line write two integer column numbers c1 andc2 (1技术分享c1,c2技术分享m,c1技术分享c2), so that values in columnsc1 andc2 are the same in rowsr1 andr2. 

Sample Input 

3 3
How to compete in ACM ICPC,Peter,peter@neerc.ifmo.ru
How to win ACM ICPC,Michael,michael@neerc.ifmo.ru
Notes from ACM ICPC champion,Michael,michael@neerc.ifmo.ru
2 3
1,Peter,peter@neerc.ifmo.ru
2,Michael,michael@neerc.ifmo.ru

Sample Output 

NO
2 3
2 3
YES

这道题应该使用map来对数据做个一一映射,先将输入的字符串通过map映射成数字,再输入到一个数组里。
之后把c1,c2两列的内容作为一个二元组存到一个map中,由于需要存入两个数据,在这里可以使用pair,在map中就是map<pair<int,int>,int>。
这儿写一下pair的用法:

makr_pair:

   pair<int ,int >p (5,6);

   pair<int ,int > p1= make_pair(5,6);

   pair<string,double> p2 ("aa",5.0);

   pair <string ,double> p3 = make_pair("aa",5.0);

有这两种写法来生成一个pair。

 

 如何取得pair的值呢。。

 每个pair 都有两个属性值  first  和second

 cout<<p1.first<<p1.second; 

 注意是属性值而不是方法。


 1 #include<iostream>
 2 #include<map>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 map<string, int> IDcache;
 8 map<pair<int, int>, int> NewIDcache;
 9 int a[11000][20];
10 int n, m;
11 
12 
13 void match()
14 {
15     int x, y;
16     for (int c1 = 0; c1 < m-1; c1++)
17     {
18         for (int c2 = c1 + 1; c2 < m; c2++)
19         {
20             for (int r = 0; r < n; r++)
21             {
22                 x = a[r][c1];
23                 y = a[r][c2];
24                 if (!NewIDcache.count(make_pair(x, y)))  NewIDcache[make_pair(x, y)] = r;
25                 else
26                 {
27                     cout << "NO" << endl;
28                     cout << NewIDcache[make_pair(x, y)] + 1 << " " << r + 1 << endl;
29                     cout << c1 + 1 << " " << c2 + 1 << endl;
30                     return;
31                 }
32             }
33             NewIDcache.clear();
34         }
35     }
36     cout << "YES" << endl;
37 }
38 
39 
40 int main()
41 {
42     while (cin >> n >> m)
43     {
44         int t = 1;
45         getchar();
46         string str;
47         IDcache.clear();
48         NewIDcache.clear();
49         for (int i = 0; i < n; i++)
50         {
51             int count = 0;
52             str.clear();
53             getline(cin, str);
54             int l = str.length();
55             string s;
56             for (int j = 0; j < l; j++)
57             {
58                 if (str[j] != ,)  s = s + str[j];
59                 if (str[j] == , || j == l-1)
60                 {
61                     if (!IDcache.count(s))  IDcache[s] = t++;
62                     a[i][count++] = IDcache[s];
63                     s.clear();
64                 }
65             }
66         }
67         match();
68     }
69 }

 

 

UVa 1592 数据库(c++pair)