cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Hoshinonono/cp-library

:heavy_check_mark: Test/Aizu Online Judge/ALDS1/ALDS1_14_C.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_14_C"

#include <bits/stdc++.h>

#include "DataStructure/CumulativeSum2D.hpp"

#include "Math/modint_61bit.hpp"

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<mint61> Y(1001), X(1001);
    Y[0] = X[0] = 1;
    for(int y = 0; y < 1000; y++){
        Y[y + 1] = Y[y] * 334;
    }
    for(int x = 0; x < 1000; x++){
        X[x + 1] = X[x] * 114514;
    }
    CumulativeSum2D<mint61> A(h, w);
    for(int y = 0; y < h; y++){
        string s;
        cin >> s;
        for(int x = 0; x < w; x++){
            A.add(y, x, Y[y] * X[x] * s[x]);
        }
    }
    A.build();

    int h2, w2;
    mint61 hash;
    cin >> h2 >> w2;
    for(int y = 0; y < h2; y++){
        string s;
        cin >> s;
        for(int x = 0; x < w2; x++){
            hash += Y[y] * X[x] * s[x];
        }
    }

    for(int y = 0; y + h2 <= h; y++){
        for(int x = 0; x + w2 <= w; x++){
            if(hash * Y[y] * X[x] == A.query(y, x, y + h2, x + w2)){
                cout << y << ' ' << x << '\n';
            }
        }
    }
}
#line 1 "Test/Aizu Online Judge/ALDS1/ALDS1_14_C.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_14_C"

#include <bits/stdc++.h>

#line 1 "DataStructure/CumulativeSum2D.hpp"
template <class T> struct CumulativeSum2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    CumulativeSum2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    CumulativeSum2D(std::vector<std::vector<T>> &A) 
        : h(A.size()), w(A[0].size()), dat(h + 1, std::vector<T>(w + 1, 0)) {
        for(int y = 1; y <= h; y++){
            for(int x = 1; x <= w; x++){
                dat[y][x] = A[y - 1][x - 1] + dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1];
            }
        }
    }
    void add(int y, int x, T z){
        assert(0 <= y && y < h);
        assert(0 <= x && x < w);
        dat[y + 1][x + 1] += z;
    }
    void build(){
        for(int y = 1; y <= h; y++) {
            for(int x = 1; x <= w; x++) {
                dat[y][x] += dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1];
            }
        }
    }
    T query(int ly, int lx, int ry, int rx){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= w);
        return dat[ry][rx] - dat[ly][rx] - dat[ry][lx] + dat[ly][lx];
    }
};
#line 1 "Math/modint_61bit.hpp"
template<const long long MOD = 2305843009213693951, 
        const long long MASK31 = (1ll << 31) - 1, const long long MASK30 = (1ll << 30) - 1>
struct modint61 {
    using mint = modint61;
    long long v;
    modint61() : v(0) {}
    template<class T> modint61(T x) { 
        while(x < 0) x += MOD;
        while(x >= MOD) x -= MOD;
        v = x;
    }
    long long safe_mod(long long x){
        x = (x >> 61) + (x & MOD);
        if (x >= MOD) x -= MOD;
        return x;
    }
    static constexpr long long mod() { return MOD; }
    mint& operator++() {v++; if(v == MOD)v = 0; return *this;}
    mint& operator--() {if(v == 0)v = MOD; v--; return *this;}
    mint operator++(int) { mint result = *this; ++*this; return result; }
    mint operator--(int) { mint result = *this; --*this; return result; }
    mint& operator+=(const mint& rhs) { v += rhs.v; if(v >= MOD) v -= MOD; return *this; }
    mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; }
    mint& operator*=(const mint& rhs) {
        long long u = v >> 31, d = v & MASK31;
        long long ru = rhs.v >> 31, rd = rhs.v & MASK31;
        long long mid = d * ru + u * rd;
        v = safe_mod(u * ru * 2 + (mid >> 30) + ((mid & MASK30) << 31) + d * rd);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    mint pow(long long n) const {
        assert(0 <= n);
        mint r = 1, x = *this;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const { assert(v); return pow(MOD - 2); }
    friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; }
    friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; }
    friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; }
    friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; }
    friend bool operator<(const mint& lhs, const mint& rhs) { return lhs.v < rhs.v; }
    friend bool operator==(const mint& lhs, const mint& rhs) { return (lhs.v == rhs.v); }
    friend bool operator!=(const mint& lhs, const mint& rhs) { return (lhs.v != rhs.v); }
    friend std::istream& operator >> (std::istream& is, mint& rhs) noexcept {
        long long tmp;
        rhs = mint{(is >> tmp, tmp)};
        return is;
    }
    friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; }
};
using mint61 = modint61<2305843009213693951>;
#line 6 "Test/Aizu Online Judge/ALDS1/ALDS1_14_C.test.cpp"
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<mint61> Y(1001), X(1001);
    Y[0] = X[0] = 1;
    for(int y = 0; y < 1000; y++){
        Y[y + 1] = Y[y] * 334;
    }
    for(int x = 0; x < 1000; x++){
        X[x + 1] = X[x] * 114514;
    }
    CumulativeSum2D<mint61> A(h, w);
    for(int y = 0; y < h; y++){
        string s;
        cin >> s;
        for(int x = 0; x < w; x++){
            A.add(y, x, Y[y] * X[x] * s[x]);
        }
    }
    A.build();

    int h2, w2;
    mint61 hash;
    cin >> h2 >> w2;
    for(int y = 0; y < h2; y++){
        string s;
        cin >> s;
        for(int x = 0; x < w2; x++){
            hash += Y[y] * X[x] * s[x];
        }
    }

    for(int y = 0; y + h2 <= h; y++){
        for(int x = 0; x + w2 <= w; x++){
            if(hash * Y[y] * X[x] == A.query(y, x, y + h2, x + w2)){
                cout << y << ' ' << x << '\n';
            }
        }
    }
}
Back to top page