Submission #695344

# Submission time Handle Problem Language Result Execution time Memory
695344 2023-02-05T04:13:51 Z Nhoksocqt1 Savez (COCI15_savez) C++17
120 / 120
164 ms 64832 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int NMOD = 2;
const int MAXN = 2000006;

const int MOD[] = {int(1e9) + 2277, int(1e9) + 5277, int(1e9) + 8277, int(1e9) + 9277};
const int BASE = 256;

ll pw[NMOD][MAXN];

struct Hash {
    ll value[NMOD];

    Hash(char c = 0) {
        for (int i = 0; i < NMOD; ++i)
            value[i] = c;
    }

    Hash operator + (const Hash &h) const {
        Hash res;
        for (int i = 0; i < NMOD; ++i) {
            res.value[i] = value[i] + h.value[i];
            if(res.value[i] >= MOD[i])
                res.value[i] -= MOD[i];
        }

        return res;
    }

    Hash operator - (const Hash &h) const {
        Hash res;
        for (int i = 0; i < NMOD; ++i) {
            res.value[i] = value[i] - h.value[i];
            if(res.value[i] < 0)
                res.value[i] += MOD[i];
        }

        return res;
    }

    Hash operator * (int k) const {
        Hash res;
        for (int i = 0; i < NMOD; ++i)
            res.value[i] = value[i] * pw[i][k] % MOD[i];

        return res;
    }

    bool operator == (const Hash &h) const {
        Hash res;
        for (int i = 0; i < NMOD; ++i) {
            if(value[i] != h.value[i])
                return false;
        }

        return true;
    }
} h[MAXN];

struct HashPair {
    ll operator() (const Hash &h) const {
        return h.value[0] * MOD[1] + h.value[1];
    }
};

inline Hash getHash(int l, int r) {
    return (h[r] - h[l - 1]) * (MAXN - r);
}

unordered_map<Hash, int, HashPair> dp;

int numString;

void init(void) {
    for (int i = 0; i < NMOD; ++i) {
        pw[i][0] = 1;
        for (int j = 1; j < MAXN; ++j)
            pw[i][j] = pw[i][j - 1] * BASE % MOD[i];
    }
}

void process() {
    cin >> numString;

    init();
    int res(0);
    for (int i = 1; i <= numString; ++i) {
        string str;
        cin >> str;

        int strLen(str.size());
        for (int j = 1; j <= strLen; ++j) {
            h[j] = h[j - 1] + Hash(str[j - 1]) * j;
        }

        int dpm(0);
        for (int j = 1; j <= strLen; ++j) {
            if(getHash(1, j) == getHash(strLen - j + 1, strLen))
                dpm = max(dpm, dp[getHash(1, j)]);
        }

        dp[getHash(1, strLen)] = 1 + dpm;
        res = max(res, 1 + dpm);
    }

    cout << res;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//    freopen("savez.inp", "r", stdin);
//    freopen("savez.out", "w", stdout);

    process();
    return 0;
}

Compilation message

savez.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O3")
      | 
savez.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 44 ms 62908 KB Output is correct
2 Correct 49 ms 62920 KB Output is correct
3 Correct 47 ms 62824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 62868 KB Output is correct
2 Correct 47 ms 62912 KB Output is correct
3 Correct 50 ms 63016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 63936 KB Output is correct
2 Correct 90 ms 63944 KB Output is correct
3 Correct 98 ms 63948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 62992 KB Output is correct
2 Correct 80 ms 63912 KB Output is correct
3 Correct 113 ms 64084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 63884 KB Output is correct
2 Correct 91 ms 64012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 63864 KB Output is correct
2 Correct 79 ms 63968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 63840 KB Output is correct
2 Correct 101 ms 63924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 63944 KB Output is correct
2 Correct 82 ms 63936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 64020 KB Output is correct
2 Correct 101 ms 64068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 64192 KB Output is correct
2 Correct 128 ms 64264 KB Output is correct
3 Correct 164 ms 64832 KB Output is correct