UVa1592, Database

思路:

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值