数据库 uva1592 database

这道题我学到了很多东西,首先是udebug,使用freopen函数,将输出重定向到文件中,这样可以将输出与输入分离,debug的时候可以用来比较输出的结果,其次是map的嵌套使用,make_pair,还有就是map的使用过程中一定要保证键要不一样,值可以一样,如果无法做到就将前两个嵌套为第一个,第二个可以取一个随便一样的值。
总体的思想就是类似于算法入门经典的思路一样,建立一个vector存储用逗号分割的字符串,使用count为每个字符串建立编号,使用map建立字符串与编号之间的关系,再使用map存储一行中两列的字符串,遇到新的两列字符串时,检测在map中是否存在,如果存在就no,到最后不存在就yes。但是这个map不符合键要唯一,所以进行了嵌套pair。

#include <bits/stdc++.h>

using namespace std;
map<string, int> m1;
int counter;
int getid(string s)
{
    if(m1.find(s) != m1.end())
    {
        return m1[s];
    }
    m1.insert({s, counter});
    return m1[s];
}
int main()
{
    int n, m;
    //FILE* f = freopen("out.txt", "w", stdout);
    while(cin >> n >> m)
    {
        getchar();
        bool ispnf = true;
        vector<string> v[n];
        m1.clear();
        map<pair<int ,int>,int> m2;
        counter = 0;
        string s1;
        for(int i = 0; i < n; ++i)
        {
            getline(cin, s1);
            stringstream ss(s1);
            string s2;
            while(getline(ss, s2, ','))
            {
                if(m1.find(s2) == m1.end())
                {
                    int id = getid(s2);
                    m1.insert({s2,counter});
                }
                v[i].push_back(s2);
                ++counter;
            }
        }
        for(int i = 0; i < m-1; ++i)
        {
            for(int j = i+1; j < m; ++j)//两列
            {
                for(int r = 0; r < n; ++r)
                {
                    string s3 = v[r][i];
                    string s4 = v[r][j];
                    //cout << s3 << "  " << s4 << endl;
                    int num3 = m1[s3];
                    int num4 = m1[s4];
                    //cout << num3 <<"   " << num4 << endl;
                    if(!(m2.count(make_pair(num3,num4)) !=0))
                    {
                        //cout << m2.count(num3) << endl;
                        //cout << m2[num3] << "  " << num4 << endl;
                        m2.insert({make_pair(num3, num4),1});

                        //cout<<num3<<"  "<< num4 <<endl;

                    }
                    else
                    {
                        //cout << num3 <<"   " << num4 << endl;

                        for(int k = 0; k < n; ++k)
                        {
                            if(v[k][i] == v[r][i] && v[k][j] == v[r][j] && k != r)
                            {
                                ispnf = false;
                                printf("NO\n");
                                printf("%d %d\n", k+1, r+1);
                                printf("%d %d\n", i+1, j+1);
                                goto lable;
                            }

                        }
                    }
                }
            }
        }
        if(ispnf)
        {
            printf("YES\n");
        }
        lable:;
    }
    //fclose(f);
    return 0;
}
/*
2 3
a,b,c
a,d,c
2 3
a,b,c
b,d,c
*/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值