Submission #718224

# Submission time Handle Problem Language Result Execution time Memory
718224 2023-04-03T17:17:11 Z Ahmed57 Rima (COCI17_rima) C++14
28 / 140
337 ms 262144 KB
#include <bits/stdc++.h>
using namespace std ;
//TRIE
struct node{
   node *nxt[26];
   int is = 0;
   node(){
      is = 0;
      for(int i = 0;i<26;i++){
        nxt[i] = NULL;
      }
   }
};
void inser(string w,node *root){
    for(auto ch:w){
        if(root->nxt[ch-'a']==NULL){
            root->nxt[ch-'a'] = new node();
        }
        root=root->nxt[ch-'a'];
    }
    root->is++;
}
int Nnode = 0;
vector<pair<int,int>> adj[3000001];
void build(node *root){
    int cur = Nnode;
    for(int j = 0;j<26;j++){
        if(root->nxt[j]!=NULL){
            Nnode++;
            adj[cur].push_back({Nnode,root->nxt[j]->is});
            //cout<<root->nxt[j]->is<<endl;
            build(root->nxt[j]);
        }
    }
}
long long dp[3000001][2];
long long calc(int node,int la,int ch = 0){
    if(dp[node][ch]!=-1)return dp[node][ch];
    long long ma = 0 , sum = la;
    ch = (ch|(la>0));
    for(auto j:adj[node]){
        sum+=j.second;
    }
    for(auto j:adj[node]){
        if(j.second>0||!ch){
            ma = max(ma,calc(j.first,j.second));
        }
    }
    return dp[node][ch] = max(sum,ma+la);
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
    node *root = new node();
    while(n--){
        string s;cin>>s;
        reverse(s.begin(),s.end());
        inser(s,root);
    }
    build(root);
    memset(dp,-1,sizeof dp);
    long long all =0;
    for(int i = 0;i<=Nnode;i++){
        all = max(all,calc(i,0,0));
    }
    cout<<all<<endl;
    return 0 ;
}
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 117712 KB Output isn't correct
2 Correct 52 ms 117780 KB Output is correct
3 Incorrect 56 ms 117748 KB Output isn't correct
4 Runtime error 337 ms 262144 KB Execution killed with signal 9
5 Correct 80 ms 121292 KB Output is correct
6 Incorrect 146 ms 222292 KB Output isn't correct
7 Incorrect 137 ms 222212 KB Output isn't correct
8 Incorrect 147 ms 221836 KB Output isn't correct
9 Incorrect 156 ms 227720 KB Output isn't correct
10 Incorrect 136 ms 221828 KB Output isn't correct