UVa1592_数据库

#include<iostream>
#include<cstdio>
#include<vector>
#include<utility>
#include<map>
#include<sstream>
#include<string> 
using namespace std;

typedef pair<int, int> PII;

const int maxr = 10000 + 5;
const int maxc = 10 + 5;

int m, n, db[maxr][maxc], cnt;  //m为当前的行数,n为当前列数,db储存所有的id值,cnt形成id

map<string, int> id;            //将先将字符串映射成id值 
int ID(const string& s)         //本题重要思想!转换成id值 
{
    if (! id.count(s))      //如果字符串s的数目为0
    {
        id[s] = ++cnt;      //形成id值 
    }
    return id[s];           //否则直接返回对应的id值
}

//相邻两列情况下 枚举每行 
void find()              
{
    for (int c1 = 0; c1 < m; c1++)
    {
        for (int c2 = c1+1; c2 < m; c2++) //相邻两列向每行枚举
        {
            map<PII, int> d;              //将二元组映射成行数
            for (int i = 0; i < n; i++)   //枚举每行
            {
                PII p = make_pair(db[i][c1], db[i][c2]);//两列的id值形成一个二元组 
                //make_pair()能从两个变量构造一个pair形成一个二元组
                if (d.count(p))    {         //二元组出现的次数不止一次 
                    cout << "No\n";       
                    cout << d[p] + 1 << ' ' << i+1 << endl;  //打印行(2行)
                    cout << c1 + 1 << ' ' << c2+1 << endl;   //打印两列
                    return; 
                }
                d[p] = i;    //否则将该行(key)映射到相应的二元组中去
             } 
        }
        cout << "YES\n" << endl; 
    }
}

int main()
{
    string s;
    while (getline(cin, s)) 
    {
        stringstream ss(s);
        if (! (ss >> n >> m))   //行列输入错误,break; 
            break;
        cnt = 0;               //每组数据形成id的值初始化为0 
        id.clear();            //每次都清空数据
        for (int i = 0; i < n; i++) {
            getline(cin, s);   //输入一行字符串,每列用 “,”隔开
            int lastpos = -1;
            for (int j = 0; j < m; j++) {
                int p = s.find(',', lastpos + 1);  //从lastpos+1开始,寻找在','
                                             //在字符串第一次出现的位置--相当于每列结束
                if (p == string::npos)       //最后一列之后没有逗号 
                    p = s.length();     
                //将一列的开始到该列结束位置的字符串形成id值储存在db中 
                db[i][j] = ID(s.substr(lastpos+1, p-lastpos-1)); 
                lastpos = p;    //将本列字符串最后的位置置为lastpos,等下一次开始位置即为下一列 
            } 
        }
        find(); 
    }
    return 0;
}

 

posted @ 2016-04-20 20:26  douzujun  阅读(383)  评论(0编辑  收藏  举报