답안 #1030404

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030404 2024-07-22T04:50:10 Z caterpillow Savez (COCI15_savez) C++17
108 / 120
86 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end() 
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;

/*

if a < b and b < c, then a < c
use string hashing

*/

const int m = 27;
const ll mod = 1e9 + 7;
const ll mx = 2e6 + 1;
ll powm[mx];

int n;
vt<vt<int>> adj;
vt<int> dp;
map<ll, int> seen;

void push_back(pl& hash, int c) {
    hash = {(hash.f * m + c + 1) % mod, hash.s + 1};
}

void push_front(pl& hash, int c) {
    hash = {(powm[hash.s] * (c + 1) + hash.f) % mod, hash.s + 1};
}

const pl hid = {0, 0};

int dfs(int u) {
    if (dp[u] != -1) return dp[u];
    int res = 0;
    for (int v : adj[u]) {
        res = max(res, dfs(v));
    }
    return dp[u] = res + 1;
}

main() {
    cin.tie(0)->sync_with_stdio(0);

    powm[0] = 1;
    FOR (i, 1, mx) powm[i] = (powm[i - 1] * m) % mod;

    cin >> n;
    adj.resize(n);
    dp.resize(n, -1);
    F0R (i, n) {
        pl lhs = hid, rhs = hid;
        string st; cin >> st;
        vt<int> a(size(st)); F0R (i, size(st)) a[i] = st[i] - 'A';
        F0R (j, size(st)) {
            push_back(lhs, a[j]);
            push_front(rhs, a[size(st) - j - 1]);
            if (lhs.f == rhs.f) {
                auto it = seen.find(lhs.f);
                if (it != seen.end()) {
                    adj[it->s].pb(i);
                }
            }
        }
        seen[lhs.f] = i;
    }
    F0R (i, n) dfs(i);
    cout << *max_element(all(dp)) << endl;
}

Compilation message

savez.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   58 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 15964 KB Output is correct
2 Correct 14 ms 15964 KB Output is correct
3 Correct 14 ms 16104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 15964 KB Output is correct
2 Correct 14 ms 15964 KB Output is correct
3 Correct 14 ms 16220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 16788 KB Output is correct
2 Correct 20 ms 16768 KB Output is correct
3 Correct 30 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 16196 KB Output is correct
2 Correct 21 ms 17244 KB Output is correct
3 Correct 24 ms 17056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 18536 KB Output is correct
2 Correct 22 ms 18780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 20304 KB Output is correct
2 Correct 28 ms 21076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 21076 KB Output is correct
2 Correct 29 ms 22848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 22864 KB Output is correct
2 Correct 34 ms 25796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 36728 KB Output is correct
2 Correct 66 ms 42476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 50004 KB Output is correct
2 Correct 72 ms 60100 KB Output is correct
3 Runtime error 86 ms 65536 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -