답안 #632405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
632405 2022-08-19T23:43:45 Z Neos Savez (COCI15_savez) C++14
120 / 120
116 ms 6092 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ii = pair<ll, ll>;

#define fastIO ios::sync_with_stdio(0); cin.tie(0);
#define fi first
#define se second
#define pb push_back
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define MASK(x) 1ll << (x)

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        X eps = 1e-9;
        if (x > y + eps) {
            x = y;
            return true;
        } else return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        X eps = 1e-9;
        if (x + eps < y) {
            x = y;
            return true;
        } else return false;
    }

const int N = 2e6 + 7;
const int base = 311, MOD = 1e9 + 7;

int n;
vector<int> dp;
string s;
map<int, int> mp;

struct Hash {
    string s;
    int n;
    vector<int> hsh, pw;

    void init(string x) {
        s = x; n = sz(s); s = ' ' + s;

        hsh.resize(n + 1, 0); pw.resize(n + 1, 1);
        for(int i = 1; i <= n; i++) {
            pw[i] = 1ll * pw[i - 1] * base % MOD;
            hsh[i] = (1ll * hsh[i - 1] * base % MOD + s[i]) % MOD;
        }
    }

    int get(int l, int r) {
        return (hsh[r] - 1ll * hsh[l - 1] * pw[r - l + 1] % MOD + MOD) % MOD;
    }
} H;
int ans = 0, tmp = 1;

signed main() {
    fastIO;
    if (fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    if (fopen("savez.inp", "r")) {
        freopen("savez.inp", "r", stdin);
        freopen("savez.out", "w", stdout);
    }
    cin >> n;
    dp.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> s; H.init(s); tmp = 1;
        for (int j = 1; j <= sz(s); j++) {
            if (H.get(1, j) == H.get(sz(s) - j + 1, sz(s)) && mp.find(H.get(1, j)) != mp.end()) {
                tmp = max(tmp, dp[mp[H.get(1, j)]] + 1);
            }
        }
        maximize(ans, tmp); mp[H.get(1, sz(s))] = i; dp[mp[H.get(1, sz(s))]] = tmp;
    }
    cout << ans;
}

Compilation message

savez.cpp: In function 'int main()':
savez.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
savez.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
savez.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("savez.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
savez.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("savez.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 1304 KB Output is correct
2 Correct 27 ms 1336 KB Output is correct
3 Correct 58 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 34 ms 1444 KB Output is correct
3 Correct 37 ms 1344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 1260 KB Output is correct
2 Correct 27 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 1404 KB Output is correct
2 Correct 25 ms 1308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 1388 KB Output is correct
2 Correct 26 ms 1404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 1648 KB Output is correct
2 Correct 30 ms 1636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 2400 KB Output is correct
2 Correct 61 ms 2508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 3160 KB Output is correct
2 Correct 71 ms 3144 KB Output is correct
3 Correct 116 ms 6092 KB Output is correct