結果
| 問題 | 
                            No.962 LCPs
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2019-12-22 12:04:45 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 18 ms / 2,000 ms | 
| コード長 | 1,715 bytes | 
| コンパイル時間 | 1,139 ms | 
| コンパイル使用メモリ | 108,884 KB | 
| 実行使用メモリ | 9,612 KB | 
| 最終ジャッジ日時 | 2024-09-14 02:31:37 | 
| 合計ジャッジ時間 | 3,436 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 64 | 
ソースコード
// O(S^1.5)
// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n;
  cin >> n;
  string t;
  cin >> t;
  const int S = 200000;
  assert(1 <= t.size() && t.size() <= S);
  for(auto c : t) assert('a' <= c && c <= 'z');
  vector<vector<int>> cnt(S + 1);
  ll ans = t.size();
  assert(ans <= S);
  for(int i = 1; i < n; i++) {
    string s;
    cin >> s;
    for(auto c : s) assert('a' <= c && c <= 'z');
    assert(1 <= s.size() && s.size() <= S);
    int f = 0;
    while(f < s.size() && f < t.size() && s[f] == t[f]) f++;
    t = s;
    cnt.at(f).push_back(i);
    ans += s.size();
    assert(ans <= S);
  }
  vector<int> used(n+1);
  used[0] = used[n] = 1;
  for(int i = 0; i <= S; i++) if(cnt[i].size()) {
    int now = 0;
    int last = 0, last2;
    int lasti = -1;
    ll x, x2;
    for(int k = 1; k <= n; k++) {
      if(now < cnt[i].size() && cnt[i].at(now) == k) {
        used[k] = 1;
        x2 = k - last;
        last2 = last;
      }
      if(lasti >= 0 && used[k]) {
        ll y = k - lasti;
        ans += ((x * x * y + x * y * y) / 2 + x * y) * i;
        lasti = -1;
      }
      if(used[k]) last = k;
      if(now < cnt[i].size() && cnt[i].at(now) == k) {
        now++;
        x = x2;
        last = last2;
        lasti = k;
      }
    }
  }
  cout << ans << endl;
  return 0;
}