CF1902E.Collapsing Strings
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
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)∣.
给你 n 个字符串 s1,s2,…,sn,均由小写拉丁字母组成。记 ∣x∣ 表示字符串 x 的长度。
定义两个字符串 a 和 b 的坍缩(collapse)操作 C(a,b) 如下:
- 若 a 为空,则 C(a,b)=b;
- 若 b 为空,则 C(a,b)=a;
- 若 a 的最后一个字母等于 b 的第一个字母,则 C(a,b)=C(a1,∣a∣−1,b2,∣b∣),其中 sl,r 表示字符串 s 从第 l 个字母到第 r 个字母的子串;
- 否则,C(a,b)=a+b,即两个字符串的拼接。
请计算 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.
第一行包含一个整数 n(1≤n≤106)。
接下来的 n 行中,每行包含一个字符串 si(1≤∣si∣≤106),由小写拉丁字母组成。
所有字符串的总长度不超过 106。
输出格式
Print a single integer — i=1∑nj=1∑n∣C(si,sj)∣.
输出一个整数——i=1∑nj=1∑n∣C(si,sj)∣。
输入输出样例
输入#1
3 aba ab ba
输出#1
20
输入#2
5 abab babx xab xba bab
输出#2
126
输入解题思路,AI测评打分。不知道怎么写?