UVA ~ 1592 ~ Database (枚举 + map + pair)

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

思路:我们先把每个字符串用map映射成一个数字,用数组存起来。然后枚举r1,r2,c1,c2明显超时。我们可以枚举c1和c2,然后对于每一行r,把c1,c2的内容用pair放入map存起来,如果这个二元组出现过,那么就找到了答案。

注意:找到答案了跳出,不跳出会wa。。。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10000 + 5;
const int MAXM = 10 + 5;
int n, m, a[MAXN][MAXM];
int main()
{
    while (cin >> n >> m)
    {
        map<string, int> ID;
        int cnt = 0;
        cin.get();
        for (int i = 0; i < n; i++)
        {
            string s;
            getline(cin, s);
            int len = s.length();
            string t;
            int m_cnt = 0;
            for (int j = 0; j < len; j++)
            {
                if (s[j] != ',') t += s[j];
                if (s[j] == ',' || j == len - 1)
                {
                    if (!ID.count(t)) ID[t] = cnt++;
                    a[i][m_cnt++] = ID[t];
                    t = "";
                }
            }
        }
        bool flag = true;
        for (int i = 0; i < m; i++)
        {
            for (int j = i + 1; j < m; j++)
            {
                map<pair<int, int>, int> M;
                for (int k = 0; k < n; k++)
                {
                    if (M.count(make_pair(a[k][i],a[k][j])))
                    {
                        cout << "NO" << endl;
                        cout << M[make_pair(a[k][i], a[k][j])] + 1 << " " << k + 1 << endl;
                        cout << i + 1 << " " << j + 1 << endl;
                        flag = false;
                        break;
                    }
                    else
                    {
                        M[make_pair(a[k][i],a[k][j])] = k;
                    }
                }
                if(!flag) break;
            }
            if(!flag) break;
        }
        if(flag) cout << "YES" << endl;
    }
    return 0;
}
/*
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
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值