思路:
for each (c1, c2) pair from columns
{
reset 'saved pairs'
for each row
{
get current pair (data in (c1, r) and data in (c2, r))
if (current pair does not exist in 'saved pairs') save current pair with its row number.
else
{
output the row number of the existing same pair in 'saved pairs'
output the current row number
output c1, c2
}
}
}
代码
#include <iostream>
#include <cmath>
#include <ctime>
#include <climits>
#include <iomanip>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <iterator>
#include <queue>
using namespace std;
const int MAXR = 10000 + 10, MAXC = 10 + 10, BASE = 1000;
int n, m;
map<string, int> map_table; //Map every unique string to an ID;
vector<string> cache; //Cache every unique string.
int table[MAXR][MAXC]; //Store the actual database.
int findID(string s)
{
if (map_table.count(s))
return map_table[s];
cache.push_back(s);
return map_table[s] = cache.size() - 1;
}
struct node
{
int x, y;
node(int x, int y) : x(x), y(y) {}
bool operator<(const node &p) const { return x < p.x || x == p.x && y < p.y; }
};
map<node, int> dat;
void proc()
{
int c1, c2, x, y;
for (c1 = 0; c1 < m; c1++)
for (c2 = c1 + 1; c2 < m; c2++)
{
dat.clear();
for (int r = 0; r < n; r++)
{
int x = table[r][c1], y = table[r][c2];
node tmp(x, y);
if (!dat.count(tmp))
dat[tmp] = r;
else
{
cout << "NO" << endl
<< dat[tmp] + 1 << " " << r + 1 << endl
<< c1 + 1 << " " << c2 + 1 << endl;
return;
}
}
}
cout << "YES" << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> n >> m)
{
memset(table, 0, sizeof(table));
map_table.clear();
cache.clear();
for (int i = 0; i < n; i++)
{
int cnt = -1, last_pos = 0;
string s, tmp;
do
{
getline(cin, s); //Because sometimes you get an empty string
} while (s == "");
for (int j = 0; j < s.length(); j++)
if (s[j] == ',')
{
table[i][++cnt] = findID(s.substr(last_pos, j - last_pos));
last_pos = j + 1;
if (cnt == m - 2) //If this is the (m-1)th column, then
{
table[i][++cnt] = findID(s.substr(last_pos));
break;
}
}
}
proc();
}
//end of program
return 0;
}