#include #include #include #include #include #include #include #include #include #include #include #include #include #define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i)) #define rFOR(i, a, b) for(int (i) = (b); (i) >= (a); --(i)) #define REP(i, n) FOR(i, 0, n) #define rREP(i, n) rFOR(i, 0, (n-1)) #define SORT(A) std::sort((A).begin(), (A).end()) #define ALL(A) (A).begin(), (A).end() // 座標圧縮 (for vector) : ソートしてから使うのが一般的 ; SORT(A) => COORDINATE_COMPRESSION(A) #define COORDINATE_COMPRESSION(A) (A).erase(unique((A).begin(),(A).end()),(A).end()) using lli = long long int; using pii = std::pair; void VintOut(std::vector& A){ const int n = A.size(); if(n == 0){putchar('\n'); return;} printf("%d", A[0]); for(int i = 1; i < n; ++i) printf(" %d", A[i]); putchar('\n'); } void VintOut(std::vector& A){ const int n = A.size(); if(n == 0){ putchar('\n'); return;} printf("%lld", A[0]); for(int i = 1; i < n; ++i) printf(" %lld", A[i]); putchar('\n'); } template inline bool chmin(T& a, T b){ if(b < a){ a = b; return true;} return false; } template inline bool chmax(T& a, T b){ if(a < b){ a = b; return true;} return false; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ constexpr int N = 2e5; char s[N+10], ps[N+10]; std::vector Prev, Working; int main(void){ int n; scanf("%d", &n); lli res = 0; int slen, plen = 0; Prev.resize(N+10); Working.resize(N+10); REP(_, n){ scanf("%s", s); slen = strlen(s); //Working.resize(slen); int idx = 0; int l = (slen > plen) ? plen : slen; while(idx < l){ if(s[idx] != ps[idx]) break; Working[idx] = Prev[idx] + 1; res += Working[idx] * (Working[idx] + 1) / 2; idx++; } while(idx < slen){ Working[idx] = 1; res++; idx++; } std::swap(Working, Prev); std::swap(s, ps); plen = slen; } printf("%lld\n", res); return 0; }