跳到正文

题解

题解:UVA1592 数据库 Database

残阳如血约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年5月24日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 6 分钟
获赞
2
题目
UVA1592

版本

  1. v1当前

观察题目的特殊性,容易发现 n,mn,m 是不同阶的,且 m≤10m\le 10,也许这就是突破口。

由于朴素地遍历 r1,r2,c1,c2r_1,r_2,c_1,c_2 时间复杂度达到了 O(n2m2)O(n^2m^2),显然是不行的。只枚举 r1,r2r_1,r_2 的话时间复杂度也比较大,故考虑枚举 c1,c2c_1,c_2。

再从上到下遍历每一行,记录一下当前行的两个字符串。如果遍历到后面时发现相同的,那么我么就找到了一组解。

C++
/**
 * Problem: UVA1592
 * Author:  OIer_wst
 * Date:    2025-05-24
 */

#include <bits/stdc++.h>
using lint = long long;
using ldb = long double;
using uint = unsigned int;
using ulint = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<lint, lint>;
using pcc = std::pair<char, char>;
#define lowbit(i) ((i)&(-(i)))
#define _for(i,a,b) for (int i=(a);i<(b);++i)
#define _forll(i,a,b) for (lint i=(a);i<(b);++i)
#define _forrev(i,a,b) for (int i=(a);i>(b);--i)
#define _forrevll(i,a,b) for (lint i=(a);i>(b);--i)
#define _rep(i,a,b) for (int i=(a);i<=(b);++i)
#define _repll(i,a,b) for (lint i=(a);i<=(b);++i)
#define _reprev(i,a,b) for (int i=(a);i>=(b);--i)
#define _reprevll(i,a,b) for (lint i=(a);i>=(b);--i)
struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } };
struct custom_hash_pair { custom_hash int_hasher; template <typename T1, typename T2> size_t operator()(const std::pair<T1, T2> &p) const { auto h1 = int_hasher(static_cast<uint64_t>(p.first)), h2 = int_hasher(static_cast<uint64_t>(p.second)); return h1 ^ (h2 << 1) ^ (h1 >> 1) ^ (0x9e3779b9 + (h1 << 6) + (h2 >> 2)); } };
const int N = 1e4 + 10, M = 20;

int n, m;
std::vector<std::string> a[N];
std::vector<std::string> str_cache;
std::unordered_map<std::string, int> id_cache;

int ID(const std::string &s) {
  if (id_cache.find(s) != id_cache.end()) return id_cache[s];
  str_cache.push_back(s);
  return id_cache[s] = str_cache.size() - 1;
}

void input() {
  str_cache.clear(), id_cache.clear();
  std::string str;
  _rep(i, 1, n) {
    a[i].clear();
    std::getline(std::cin, str);
    size_t lst = -1, pos = str.find(',');
    a[i].push_back("");
    while (pos != std::string::npos) {
      a[i].push_back(str.substr(lst + 1, pos - lst - 1));
      ID(a[i].back());
      lst = pos, pos = str.find(',', lst + 1);
    }
    a[i].push_back(str.substr(lst + 1));
    ID(a[i].back());
  }
}

void process() {
  _rep(c1, 1, m) _rep(c2, c1 + 1, m) {
    std::unordered_map<pii, int, custom_hash_pair> mp;
    _rep(r, 1, n) {
      auto cur = std::make_pair(ID(a[r][c1]), ID(a[r][c2]));
      if (mp.find(cur) != mp.end()) {
        std::cout << "NO\n" << mp[cur] << " " << r << "\n" << c1 << " " << c2 << "\n";
        return ;
      }
      mp[cur] = r;
    }
  }
  std::cout << "YES\n";
}

int main() {
  std::cin.tie(0)->sync_with_stdio(0);
  while (std::cin >> n >> m) {
    std::cin.ignore(INT_MAX, '\n');
    input();
    // for (const auto &i : str_cache) std::cout << i << "\n";
    process();
  }
  std::cout.flush();
  return 0;
}
原文