CF1902E.Collapsing Strings
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given n strings s1,s2,…,sn , consisting of lowercase Latin letters. Let ∣x∣ be the length of string x .
Let a collapse C(a,b) of two strings a and b be the following operation:
- if a is empty, C(a,b)=b ;
- if b is empty, C(a,b)=a ;
- if the last letter of a is equal to the first letter of b , then C(a,b)=C(a1,∣a∣−1,b2,∣b∣) , where sl,r is the substring of s from the l -th letter to the r -th one;
- otherwise, C(a,b)=a+b , i. e. the concatenation of two strings.
Calculate i=1∑nj=1∑n∣C(si,sj)∣ .
输入格式
The first line contains a single integer n ( 1≤n≤106 ).
Each of the next n lines contains a string si ( 1≤∣si∣≤106 ), consisting of lowercase Latin letters.
The total length of the strings doesn't exceed 106 .
输出格式
Print a single integer — i=1∑nj=1∑n∣C(si,sj)∣ .
输入输出样例
输入#1
3 aba ab ba
输出#1
20
输入#2
5 abab babx xab xba bab
输出#2
126