STL UVA1592

题目
UVA1592

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<vector>
using namespace std;

const int ROW=10000+10;
const int COL=10+5;
int n,m;
map<string,int> IDcache;    //字符串与数字的映射

vector<string> Strcache;    //用来给字符串映射数字
vector<int> Text[ROW];       //生成二位数组来保存编号后的数据库
struct node
{
    int x,y;
    node(int x, int y):x(x),y(y) { }
    bool operator < (const node& r) const { return x<r.x || x==r.x&&y<r.y; }
};
map<node,int> data;          //每行的映射
int ID(string str)
{
    if(IDcache.count(str))
        return IDcache[str];
    Strcache.push_back(str);
    return IDcache[str]=Strcache.size()-1;
}

void read()
{
    string str;
    char ch=getchar();
    for(int i=0;i<n;i++){
        for(;;){
            ch=getchar();
            if(ch=='\n'||ch=='\r'){
                 if(!str.empty())
                        Text[i].push_back(ID(str));
                 str.clear();
                 break;
            }
            if(ch!=',')
                str+=ch;
            else {
                Text[i].push_back(ID(str));
                str.clear();
            }
        }
    }
}

void solve()
{
    int x,y,c1,c2;
    for(c1=0;c1<m;c1++){
        for(c2=c1+1;c2<m;c2++){
            data.clear();
            for(int r=0;r<n;r++){
                x=Text[r][c1];
                y=Text[r][c2];
                node p(x,y);
                if(!data.count(p))
                    data[p]=r;
                else {
                    cout<<"NO"<<endl;
                    cout<<data[p]+1<<" "<<r+1<<endl<<c1+1<<" "<<c2+1<<endl;
                    return;
                }
            }
        }
    }
    cout<<"YES"<<endl;
}
int main()
{
    while(cin>>n>>m){
        read();
        solve();
        for(int i=0;i<n;i++)
            Text[i].clear();
        IDcache.clear();
        Strcache.clear();
    }
    return 0;
}

题意:
给一个数据库,查找是否存在(r1,c1)=(r2,c1) && (r1,c2)=(r2,c2),即:不同的二行,对应二列字符串相同
解题:
1. 首先读入字符串,将每个字符串分配一个编号,这样在遍历数据库查找时会迅速很多,不用比较字符串比如对于
3 3
How to compete in ACM ICPC,Peter,[email protected]
How to win ACM ICPC,Michael,[email protected]
Notes from ACM ICPC champion,Michael,[email protected]
编号为
0 1 2
3 4 5
6 4 5
2. 因为要找到两对相同的列,四重遍历可以找到,但是太慢了,考虑将c1,c2两列的内容一起存到map中,所以map的key为(x,y)【x,y分别代表对应字符串的编号】,map的值为对应的行r1,遍历行就是r2;所以三重遍历即可完成。
注意:
1. 注意要找到的二列,不一定相邻,可以相隔;
2. 对于map

猜你喜欢

转载自blog.csdn.net/qq_40861069/article/details/79903613