#abc343g. G - Compress Strings

G - Compress Strings

Score: 600600 points

问题描述

给定 NN 个字符串 S1,S2,,SNS_1, S_2, \ldots, S_N

找出包含所有这些字符串作为子串的最短长度的字符串。

这里,若一个字符串 SS 通过删除任意数量的起始字符和任意数量的结束字符后能够得到字符串 TT,则称字符串 SS 包含字符串 TT 作为子串。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given NN strings S1,S2,,SNS_1, S_2, \ldots, S_N.

Find the minimum length of a string that contains all these strings as substrings.

Here, a string SS contains a string TT as a substring if TT can be obtained by deleting zero or more characters from the beginning and zero or more characters from the end of SS.

Constraints

  • NN is an integer.
  • 1N201 \leq N \leq 20
  • SiS_i is a string consisting of lowercase English letters whose length is at least 11.
  • The total length of S1,S2,,SNS_1, S_2, \dots, S_N is at most 2×1052\times 10^5.

Input

The input is given from Standard Input in the following format:

NN

S1S_1

S2S_2

\vdots

SNS_N

Output

Print the answer as an integer.

Sample Input 1

3
snuke
kensho
uk

Sample Output 1

9

The string snukensho of length 99 contains all of S1S_1, S2S_2, and S3S_3 as substrings.

Specifically, the first to fifth characters of snukensho correspond to S1S_1, the fourth to ninth correspond to S2S_2, and the third to fourth correspond to S3S_3.

No shorter string contains all of S1S_1, S2S_2, and S3S_3 as substrings. Thus, the answer is 99.

Sample Input 2

3
abc
abc
arc

Sample Output 2

6

Sample Input 3

6
cmcmrcc
rmrrrmr
mrccm
mmcr
rmmrmrcc
ccmcrcmcm

Sample Output 3

27

update @ 2024/3/10 01:17:08