算法竞赛入门经典第五章例题5-9 Database UVA - 1592

https://vjudge.net/problem/UVA-1592

#include
#include
#include
#include
using namespace std;
#pragma warning(disable:4996)
int vis[10005][11];
int dat[10005][11];
map<string, int> IDCache;
vector<string> StrCache;
int ID(string s) { 
    if (IDCache.count(s)) return IDCache[s];
    StrCache.push_back(s);
    return IDCache[s] = StrCache.size()-1;
}
int r, c,pos,be,ans_c1,ans_c2,ans_r1,ans_r2;
int main(){
#ifdef _DEBUG
    freopen("in", "rb", stdin);
    freopen("out", "wb", stdout);
#endif // _DEBUG
    string st;
    while (cin >> r >> c) {
        getline(cin, st);
        for (int i = 1; i <= r; ++i) {
            getline(cin, st), be = 0;
            for (int j = 1; j <= c; ++j) {
                pos = st.find_first_of(",\r",be);
                if (pos == string::npos) pos = st.length();
                dat[i][j] = ID(st.substr(be, pos - be));
                be = pos + 1;
            }
        }
        bool _ok = false;
        for (int c1 = 1; c1 <= c; ++c1) {
            for (int c2 = c1+1; c2 <= c; ++c2) {
                mapint,int>, int> vis;
                for (int k = 1; k <= r; ++k) {
                    pair<int,int> nn(dat[k][c1], dat[k][c2]); 
                    if (vis.count(nn)) {
                        ans_c1 = c1, ans_c2 = c2, ans_r1 = vis[nn], ans_r2 = k; _ok = true; break;
                    }
                    else vis[nn] = k;
                }
                if (_ok) break;
            }
            if (_ok) break;
        }
        if (_ok) printf("NO\n%d %d\n%d %d\n", ans_r1, ans_r2, ans_c1, ans_c2);
        else printf("YES\n");
    }
}

你可能感兴趣的:(算法竞赛入门经典第五章例题5-9 Database UVA - 1592)