Блог пользователя I_need_7.0_IELTS

Автор I_need_7.0_IELTS, история, 13 месяцев назад, По-английски

Statement

Given $$$n$$$ strings. Find the string $$$s$$$ with minimum length such that each of $$$n$$$ given strings is a substring of $$$s$$$.

Constraint

$$$n \le 50$$$

Every of $$$n$$$ strings has length not exceed $$$10$$$

Example:

Input:

3
ab
ba
abb

Output:

abba

This problem is hard for me. Can you give me a hint? Thank you in advance.

  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится

»
13 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Is it similar to this question?

https://mirror.codeforces.com/gym/104049/problem/K

Because depending on the constraints it can get really hard really fast

  • »
    »
    13 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I have successfully solved the easy version of it but hard version, I can't

    • »
      »
      »
      13 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      You can see that n = 10, so that's the hint that you're able to test every combination. However how to test every combination? A quick way is with string hashing

»
13 месяцев назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

look to comment pavook

  • »
    »
    13 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Your solution is incorrect. Consider the test:

    4
    abc
    bcx
    xb 
    axa
    

    Your program returns the string abcaxabcxb, while a considerably shorter string axabcxb contains all the substrings, too.

    • »
      »
      »
      13 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      yes, you are right. I modified the code and used SCS. thx

      • »
        »
        »
        »
        13 месяцев назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        Your solution is still incorrect, though it performs a bit better on trivial tests. Consider the test:

        4
        aaa
        cae
        aec
        eee
        

        Your solution returns the string aaacaeceee, while the string aaaecaeee is shorter, but also contains all the substrings.

        If you look at my other comment, you'll understand, that designing an exact polynomial algorithm for this problem is in principle very hard.

»
13 месяцев назад, # |
Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится

This problem is known as the "Shortest common superstring" problem and is NP-hard (see Wikipedia ). This means solving it in $$$O(n^k)$$$ where $$$n$$$ is the summary length of strings, and $$$k$$$ is any fixed number, would be a major breakthrough in Computer Science.

Moreso, we can't even provably 2-approximate the answer (create an answer with length not greater than double the minimum possible).

You'll probably achieve best results using some approximate methods, e.g. simulated annealing.

  • »
    »
    13 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thank you

  • »
    »
    13 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Can't it be done by creating a suffix automaton?

    We can do something sort of topological sorting on the trie having suffix links(or failure links) and then start creating the final string which we need using dfs?

    • »
      »
      »
      13 месяцев назад, # ^ |
      Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

      That wouldn't always be an optimal solution. Indeed, there's a conjecture that a variation of the algorithm you suggested 2-approximates the solution.

      By the way, trie with suffix links is basically Aho-Corasick automaton. And it makes sense that it's possible to construct a solution with such a structure: think about it -- the Aho-Corasick algorithm basically checks character-by-character, that all the substrings are present in our string.

      • »
        »
        »
        »
        13 месяцев назад, # ^ |
        Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

        Thank You.

        By the way, if possible, please provide pseudo code for the approximation methods like simulated annealing. I am new on this,so It would be a great help.

        One of the problems similar to this :Fullmetal Alchemist II

»
13 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by I_need_7.0_IELTS (previous revision, new revision, compare).

»
13 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by I_need_7.0_IELTS (previous revision, new revision, compare).

»
13 месяцев назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

I think it's a NP-hard problem? So it's impossible to give an answer in polynomial time. You cant solve it in time using a O(K^n) algorithm :)