Submission #785952

# Submission time Handle Problem Language Result Execution time Memory
785952 2023-07-17T20:10:32 Z MilosMilutinovic Rima (COCI17_rima) C++14
42 / 140
1000 ms 18020 KB
/**
 *    author:  wxhtzdy
 *    created: 17.07.2023 22:01:28
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
  }
  sort(s.begin(), s.end(), [&](string s, string t) {
    if (s.size() != t.size()) {
      return s.size() < t.size();
    } else {
      return s < t;
    }
  });
  auto Check = [&](string s, string t) {
    int ptr = 0;
    int si = (int) s.size();
    int sj = (int) t.size();
    while (ptr < min(si, sj) && s[ptr] == t[ptr]) {
      ptr += 1;
    }     
    return (ptr >= max(si, sj) - 1);
  };
  auto Same = [&](string s, string t) {
    s.pop_back();
    t.pop_back();
    return s == t; 
  };
  vector<int> dp(n);
  for (int i = 0; i < n; i++) {
    int ptr = i;
    while (ptr + 1 < n && Same(s[i], s[ptr + 1])) {
      ptr += 1;
    }
    int cnt = ptr - i + 1; 
    int mx = 0;
    for (int j = 0; j < i; j++) {
      if (Check(s[i], s[j])) {
        mx = max(mx, dp[j]);
      }
    }
    for (int j = i; j <= ptr; j++) {
      dp[j] = mx + cnt;
    }
    i = ptr;
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';                   
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Execution timed out 1079 ms 18020 KB Time limit exceeded
5 Execution timed out 1062 ms 3452 KB Time limit exceeded
6 Incorrect 47 ms 1348 KB Output isn't correct
7 Incorrect 46 ms 1140 KB Output isn't correct
8 Incorrect 56 ms 1088 KB Output isn't correct
9 Execution timed out 1094 ms 4224 KB Time limit exceeded
10 Incorrect 36 ms 1112 KB Output isn't correct