观察题目的特殊性,容易发现 是不同阶的,且 ,也许这就是突破口。
由于朴素地遍历 时间复杂度达到了 ,显然是不行的。只枚举 的话时间复杂度也比较大,故考虑枚举 。
再从上到下遍历每一行,记录一下当前行的两个字符串。如果遍历到后面时发现相同的,那么我么就找到了一组解。
/**
* 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;
}