CF1902E.Collapsing Strings

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn strings s1,s2,,sns_1, s_2, \dots, s_n , consisting of lowercase Latin letters. Let x|x| be the length of string xx .

Let a collapse C(a,b)C(a, b) of two strings aa and bb be the following operation:

  • if aa is empty, C(a,b)=bC(a, b) = b ;
  • if bb is empty, C(a,b)=aC(a, b) = a ;
  • if the last letter of aa is equal to the first letter of bb , then C(a,b)=C(a1,a1,b2,b)C(a, b) = C(a_{1,|a|-1}, b_{2,|b|}) , where sl,rs_{l,r} is the substring of ss from the ll -th letter to the rr -th one;
  • otherwise, C(a,b)=a+bC(a, b) = a + b , i. e. the concatenation of two strings.

Calculate i=1nj=1nC(si,sj)\sum\limits_{i=1}^n \sum\limits_{j=1}^n |C(s_i, s_j)| .

输入格式

The first line contains a single integer nn ( 1n1061 \le n \le 10^6 ).

Each of the next nn lines contains a string sis_i ( 1si1061 \le |s_i| \le 10^6 ), consisting of lowercase Latin letters.

The total length of the strings doesn't exceed 10610^6 .

输出格式

Print a single integer — i=1nj=1nC(si,sj)\sum\limits_{i=1}^n \sum\limits_{j=1}^n |C(s_i, s_j)| .

输入输出样例

  • 输入#1

    3
    aba
    ab
    ba

    输出#1

    20
  • 输入#2

    5
    abab
    babx
    xab
    xba
    bab

    输出#2

    126
首页