Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- using pii = pair<int, int>;
- const int base1 = 30;
- const int base2 = 31;
- ull ubinpow(ull x, int p) {
- ull ans = 1;
- while (p) {
- if (p & 1)
- ans *= x;
- x *= x;
- p >>= 1;
- }
- return ans;
- }
- struct Coord {
- int x, y;
- };
- void solve() {
- int n, m;
- cin >> n >> m;
- vector<vector<char>> a(n, vector<char>(m));
- vector<vector<ull>> h(n + 1, vector<ull>(m + 1));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> a[i][j];
- }
- }
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- int v = a[i - 1][j - 1] - 'a';
- h[i][j] = h[i - 1][j] * base1 + h[i][j - 1] * base2 - h[i - 1][j - 1] * base1 * base2 + v;
- }
- }
- gp_hash_table<ull, Coord> found;
- for (int k = min(n, m); k >= 1; --k) {
- for (int i = k; i <= n; ++i) {
- for (int j = k; j <= m; ++j) {
- ull cur_hash = h[i][j] - h[i - k][j] * ubinpow(base1, k) - h[i][j - k] * ubinpow(base2, k);
- cur_hash += h[i - k][j - k] * ubinpow(base1 * base2, k);
- if (found.find(cur_hash) != found.end()) {
- Coord crd = found[cur_hash];
- int x = crd.x;
- int y = crd.y;
- cout << k << '\n';
- cout << x - k + 1 << ' ' << y - k + 1 << '\n';
- cout << i - k + 1 << ' ' << j - k + 1 << '\n';
- return;
- }
- found[cur_hash] = {i, j};
- }
- }
- }
- cout << 0 << '\n';
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int t = 1;
- //cin >> t;
- while (t--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement