UVa 1592 - Database

昨晚写的,学习了用map映射自定义类型,还有operator。



#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#define MAXN_N_LEN 10010
#define MAXN_M_LEN 11
#define MAXN_STR_LEN 90
using namespace std;

struct Point{
    int n1, n2;
};

struct cmp_Point {
    bool operator () (const Point &k1, const Point &k2) const {
        if(k1.n1 != k2.n1)
            return k1.n1 < k2.n1;
        if(k1.n2 != k2.n2)
            return k1.n2 < k2.n2;
        return false;
    }
};

int table[MAXN_N_LEN][MAXN_M_LEN];
char temp[MAXN_N_LEN][MAXN_STR_LEN];
string temp_str;
map<string, int> IDcache;
vector<string> ID_card;

int ID_num(string s) {
    if(IDcache.count(s)) return IDcache[s];
    ID_card.push_back(s);
    return IDcache[s] = ID_card.size() - 1;
}

int main() {
    int r, c;
    while(cin >> r >> c) {
        int sign = 0;
        getchar();
        for(int i = 0; i < r; i++)
            gets(temp[i]);
        for(int i = 0; i < r; i++) {
            int k = 0, q = 0;
            for(int j = 0; j <= strlen(temp[i]); j++)
                if(temp[i][j] == ','|| j == strlen(temp[i])) {
                    table[i][q++] = ID_num(temp_str);
                    temp_str.clear();
                }
                else
                    temp_str += temp[i][j];
        }
        for(int i = 0; i < c - 1; i++) {
            for(int j = i + 1; j < c; j++) {
                map<Point, int, cmp_Point> table_num;
                for(int k = 0; k < r; k++) {
                    Point p;
                    p.n1 = table[k][i];
                    p.n2 = table[k][j];
                    if(!table_num.count(p))
                        table_num.insert(pair<Point, int> (p, k));
                    else {
                        cout << "NO" << endl;
                        cout << table_num[p] + 1 << " " << k + 1 << endl;
                        cout << i + 1 << " " << j + 1 << endl;
                        sign = 1;
                        break;
                    }
                }
                if(sign) break;
            }
            if(sign) break;
        }
        if(!sign)
            cout << "YES" << endl;
        memset(table, 0, sizeof(table));
        memset(temp, 0, sizeof(temp));
        temp_str.clear();
        IDcache.clear();
        ID_card.clear();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值