Advertisement
hung_mine

PREFIX

Oct 11th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /// from HUNG MINE with love <3
  4. string s[1000001];
  5. long long res = 0, cnt[400001], t[400001][50], n, cc = 0, sum[1000001];
  6. int main () {
  7.       if (fopen ("test.inp", "r")) {
  8.             freopen ("test.inp", "r", stdin);
  9.       }
  10. //      else {
  11. //            freopen ("PREFIX.inp", "r", stdin);
  12. //            freopen ("PREFIX.out", "w", stdout);
  13. //      }
  14.  
  15.       ios_base :: sync_with_stdio (0);
  16.       cin.tie (0);
  17.       cout.tie (0);
  18.       cin >> n;
  19.       for (int i = 1; i <= n; ++ i) {
  20.             cin >> s[i];
  21.             int r = 0;
  22.             for (int j = 0; j < s[i].size (); ++ j) {
  23.                   if (t[r][s[i][j]] == 0) {
  24.                         t[r][s[i][j]] = ++ cc;
  25.                   }
  26.                   r = t[r][s[i][j]];
  27.             }
  28.             ++ cnt[r];
  29.       }
  30.       for (int i = 1; i <= n; ++ i) {
  31.             int r = 0;
  32.             for (int j = 0; j < s[i].size (); ++ j) {
  33.                   res += cnt[r];
  34.                   r = t[r][s[i][j]];
  35.             }
  36.       }
  37.       sum[0] = 0;
  38.       for (int i = 1; i <= n; ++ i) {
  39.             sum[i] = sum[i - 1] + i;//cout << sum[i] << " ";
  40.       }
  41.       for (int i = 0; i <= cc; ++ i) {
  42.             if (cnt[i] != 0 ) res += sum[cnt[i] - 1];
  43.       }
  44.       cout << res;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement