답안 #701225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
701225 2023-02-20T15:06:21 Z Do_you_copy Rima (COCI17_rima) C++17
70 / 140
343 ms 205184 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);

using namespace std;
using ll = long long;
using pii = pair <int, int>;

const int maxN = 3e6 + 10;
const int inf = 0x3f3f3f3f;
int n;
string s[maxN];

struct TNode{
    int a[26];
    bool ok;
    TNode(){
        memset(a, 0, sizeof(a));
        ok = 0;
    }
};

vector <TNode> S(1);
int dp[maxN];

void add(int id, int cur, int pos){
    if (pos == signed(s[cur].length())){
        S[id].ok = 1;
        return;
    }
    if (!S[id].a[s[cur][pos] - 'a']){
        S[id].a[s[cur][pos]- 'a'] = S.size();
        S.pb(TNode());
    }
    add(S[id].a[s[cur][pos] - 'a'], cur, pos + 1);
}

int ans = 0;
void dfs(int u){
    pii maxx = {0, 0};
    int cnt = 0;
    for (int i = 0; i < 26; ++i){
        if (S[u].a[i]){
            dfs(S[u].a[i]);
            cnt += bool(dp[S[u].a[i]]);
            if (maxx.fi < dp[S[u].a[i]]){
                swap(maxx.fi, maxx.se);
                maxx.fi = dp[S[u].a[i]];
            }
            else{
                maxx.se = max(maxx.se, dp[S[u].a[i]]);
            }
        }
    }
    ans = max(ans, maxx.fi + maxx.se + max(0, cnt - 2));
    if (S[u].ok) dp[u] = S[u].ok + maxx.fi + max(0, cnt - 1);
}

void Init(){
    cin >> n;
    for (int i = 1; i <= n; ++i){
        cin >> s[i];
        reverse(s[i].begin(), s[i].end());
        add(0, i, 0);
    }
    dfs(0);
    cout << ans;
}

#define taskname "test"

signed main(){
    if (fopen(taskname ".inp", "r")){
        freopen(taskname ".inp", "r", stdin);
        freopen(taskname ".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
}

Compilation message

rima.cpp: In function 'int main()':
rima.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rima.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen(taskname ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 94292 KB Output is correct
2 Correct 45 ms 94300 KB Output is correct
3 Correct 41 ms 94248 KB Output is correct
4 Correct 343 ms 205184 KB Output is correct
5 Correct 114 ms 98840 KB Output is correct
6 Incorrect 115 ms 150524 KB Output isn't correct
7 Incorrect 112 ms 150268 KB Output isn't correct
8 Incorrect 111 ms 150160 KB Output isn't correct
9 Incorrect 182 ms 155432 KB Output isn't correct
10 Incorrect 108 ms 150320 KB Output isn't correct