D. Restoration of string

Codeforces
IDCF886D
Time2000ms
Memory256MB
Difficulty
constructive algorithmsgraphsimplementation
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. ## Input 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. ## Output 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. [samples] ## Note 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.
Samples
Input #1
4
mail
ai
lru
cf
Output #1
cfmailru
Input #2
3
kek
preceq
cheburek
Output #2
NO
API Response (JSON)
{
  "problem": {
    "name": "D. Restoration of string",
    "description": {
      "content": "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 n",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF886D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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.\n\nYou are given a set of strings. A string (not n...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments