CF886D.Restoration of string

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring.

You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes).

A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string.

The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap.

String a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ.

某个字符串的子串被称为最频繁子串,当且仅当它的出现次数不小于该字符串中任意其他子串的出现次数。

给定一个字符串集合。一个字符串(不一定属于该集合)被称为好字符串,当且仅当该集合中的每个字符串都是这个字符串的最频繁子串。请构造一个非空的好字符串,使其长度最小;若存在多个满足条件的字符串,则选择其中字典序最小者。若不存在好字符串,则输出 "NO"(不带引号)。

字符串的一个子串是指该字符串中连续的一段字符序列。例如,"ab"、"c"、"abc" 都是字符串 "abc" 的子串,而 "ac" 不是 "abc" 的子串。

一个子串在字符串中的出现次数,是指该子串在字符串中作为连续子序列出现的起始位置的个数(允许重叠)。

字符串 aa 字典序小于字符串 bb,当且仅当:aa 是 bb 的前缀,或者在 aa 与 bb 第一个不同的位置上,aa 在该位置的字符比 bb 对应位置的字符小。

输入格式

The first line contains integer n (1 ≤ n ≤ 105) — the number of strings in the set.

Each of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct.

The total length of the strings doesn't exceed 105.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 表示集合中字符串的个数。

接下来的 nn 行,每行包含一个非空字符串,且仅由小写英文字母组成。保证所有字符串互不相同。

所有字符串的总长度不超过 10510^5。

输出格式

Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings.

输出长度最短的非空“好字符串”。如果存在多个这样的“好字符串”,则输出其中字典序最小的一个。如果不存在“好字符串”,则输出 "NO"(不带引号)。

输入输出样例

  • 输入#1

    4
    mail
    ai
    lru
    cf

    输出#1

    cfmailru
  • 输入#2

    3
    kek
    preceq
    cheburek

    输出#2

    NO

说明/提示

One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum.

可以证明,在第一个样例中,仅存在两个长度最短的“好字符串”:"cfmailru" 和 "mailrucf"。其中,第一个字符串字典序最小。

输入解题思路,AI测评打分。不知道怎么写?

首页