제출 #1340885

#제출 시각아이디문제언어결과실행 시간메모리
1340885doantaolaaidiSavez (COCI15_savez)C++20
120 / 120
173 ms33064 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define AC "test"
#define foru(i, l, r) for (int i = (l); i <= (r); i++)
#define ford(i, l, r) for (int i = (l); i >= (r); i--)
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;

const ll inf = 1e9 + 7;
const ll linf = 1e18 + 7;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 7;
const int base = 31;

void fastIO(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

ll mul(ll a, ll b){
    a%=mod;
    ll res = 0;
    while (b){
        if (b%2) res = (res + a)%mod;
        a = (a + a)%mod;
        b/=2;
    }
    return res;
}

ll Pow(ll a, ll b){
    ll ans = 1;
    while (b){
        if (b % 2) ans = mul(ans, a);
        a = mul(a, a);
        b/=2;
    }
    return ans;
}

struct Node{
    Node* child[26];
    int cnt;
    Node(){
        for (int i = 0; i < 26; i++) child[i] = NULL;
        cnt = 0;
    }
};

Node*root = new Node();
ll n, hashs[maxn], val, res, pow1[maxn];
string s;

ll gethash(int i, int j){
    return (hashs[j] - mul(hashs[i-1], pow1[j-i+1]) + mod)%mod;
}

void update(string t, ll tmp){
    Node*u = root;
    for (char c: t){
        if (u->child[c - 'A'] == NULL) u->child[c - 'A'] = new Node();
        u = u->child[c - 'A'];
    }
    u->cnt = tmp;
}

ll get(string t){
    ll pre = 0;
    ll ans = 0;
    Node* u = root;
    foru(i, 0, t.size() - 1){
        if (u->child[t[i] - 'A'] == NULL) return ans;
        u = u->child[t[i] - 'A'];
        pre = (pre * 31 + t[i] - 'A' + 1) % mod;
        ll suf = gethash(t.size() - i, t.size());
        if (pre != suf) continue;
        ans = max(ans, (ll)u->cnt);
    }
    return ans;
}

void solve(){
    pow1[0] = 1;
    cin >> n;
    foru(i, 1, 2000000){
        pow1[i] = (pow1[i - 1] * 31) % mod;
    }
    res = 0;
    foru(j, 1, n){
        cin >> s;
        hashs[0] = 0;
        foru(i, 1, s.size()){
            hashs[i] = (hashs[i - 1] * 31 + s[i - 1] - 'A' + 1) % mod;
        }
        val = get(s) + 1;
        res = max(res, val);
        update(s, val);
    }
    cout << res;
}

int main(){
    fastIO();
    if (fopen(AC".inp", "r")){
        freopen(AC".inp", "r", stdin);
        freopen(AC".out", "w", stdout);
    }
    solve();
}

컴파일 시 표준 에러 (stderr) 메시지

savez.cpp: In function 'int main()':
savez.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen(AC".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
savez.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen(AC".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...