Submission #994043

#TimeUsernameProblemLanguageResultExecution timeMemory
994043vjudge1Rima (COCI17_rima)C++17
56 / 140
276 ms137384 KiB
#include<bits/stdc++.h>

using namespace std;

struct node
{
  node * ch[26] = {NULL};
  bool e = false;
  int dp;

  int dfs()
  {
    // cerr << "I'm" << endl;
    int ans = 0;
    int mx = 0;
    dp = e;
    for(int i = 0; i < 26; i++)
      {
	if(ch[i] == NULL) continue;

	// cerr << "---> " << char(i + 'a') << endl;
	ans = max(ans, ch[i]->dfs());
	// cerr << ch[i] -> dp << endl;
	// cerr << "<---" << char(i + 'a') << endl;
	if(ch[i] -> e)
	  {
	    mx = max(mx, ch[i]->dp - 1);
	    dp++;
	  }
      }
    
    dp += mx;
    ans = max(ans, dp);
    return ans;
  }
};

struct Trie
{
  node * root;
  Trie() {
    root = new node();
  }

  void insert(string &s)
  {
    node *cur = root;
    for(int i = s.size() - 1; i >= 0; i--)
      {
	int idx = s[i] - 'a';
	if(cur -> ch[idx] == NULL)
	  cur -> ch[idx] = new node();
	cur = cur -> ch[idx];
      }
    cur -> e = true;
  }

  int compute()
  {
    return root->dfs();
  }
  
} T;

int main()
{
  int n;
  cin >> n;
  for(int i = 0; i < n; i ++)
    {
      string s;
      cin >> s;
      T.insert(s);
    }
  cout << T.compute() << endl;
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...