Блог пользователя brownfox2k6

Автор brownfox2k6, история, 3 года назад, По-английски

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
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится 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

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

look to comment pavook

  • »
    »
    3 года назад, скрыть # ^ |
    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.

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

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

      • »
        »
        »
        »
        3 года назад, скрыть # ^ |
        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.

»
3 года назад, скрыть # |
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.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится +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 :)