算典05_例题_09_UVA-1592

Database

题意

    输入一个n行m列的数据库(1≤n≤10000,1≤i≤10),是否存在两个不同行r1,r2和两个不同列c1,c2,使得这两行和这两列相同(即(r1,c1)和(r2,c1)相同,(r1,c2)和(r2,c2)相同)。

题解

直接四层循环绝对会超时,不是吓唬你
列数比较少(<=10),遍历两个列数比较好
然后从上到下遍历每一行,用一个map映射两列上的字符串到一个行数
如果直接将两列的字符串拼接起来,然后用map < string,  int >来映射的话,也会超时
所以在这之前最好将字符串映射到一个整数,即相同的字符串有相同的id,这个可以用一个int数组和一个map < string, int >结合着来实现,具体请看下面的代码

//这里a[i][j]即为字符串input[i][j]映射到的id
//map的作用就是保证字符串相等的id也相等
if(!maps.count(input[i][j])) maps[input[i][j]] = aid++;
a[i][j] = maps[input[i][j]];

注意上面几点这题就可以过了

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a, b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
using namespace std;
typedef long long LL;
const int maxn = 1e4 + 5;
const int INF = (1 << 31) - 1;

int n, m, a[maxn][15], aid;
string s[maxn];
char input[maxn][15][100];

struct node {
    int x, y;
    node(){}
    node(int _x, int _y):x(_x), y(_y){}
    bool operator < (const node& b)const {
        if(x == b.x) return y < b.y;
        return x < b.x;
    }
};

map<string, int> maps;
map<node, int> res;

void solve() {
    for(int i = 0; i < m; ++i) {
        for(int j = i+1; j < m; ++j) {
            res.clear();
            for(int k = 0; k < n; ++k) {
                node tmp(a[k][i], a[k][j]);
                if(!res.count(tmp)) res[tmp] = k;
                else {
                    int t = res[tmp];
                    cout << "NO" <<endl;
                    cout << t+1 << " " << k+1 << endl;
                    cout << i+1 << " " << j+1 << endl;
                    return;
                }
            }
        }
    }
    cout << "YES" <<endl;
}

int main() {
    #ifdef _LOCAL
    IN;
    #endif // _LOCAL

    while(scanf("%d%d", &n, &m) == 2) {
        getchar(); aid = 0;
        maps.clear();
        for(int i = 0; i < n; ++i) {
            getline(cin, s[i]);
            int len = s[i].length(), k = 0, kk = 0;
            for(int j = 0; j < len; ++j) {
                if(s[i][j] != ',') input[i][k][kk++] = s[i][j];
                else input[i][k][kk] = '\0', kk = 0, ++k;
            }
            input[i][k][kk] = '\0';

            for(int j = 0; j <= k; ++j) {
                if(!maps.count(input[i][j])) maps[input[i][j]] = aid++;
                a[i][j] = maps[input[i][j]];
            }
        }
        solve();
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值