Блог пользователя problem-solved

Автор problem-solved, 12 лет назад, По-английски
  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
12 лет назад, # |
Rev. 2   Проголосовать: нравится -7 Проголосовать: не нравится

Well, an interesting problem...

But I've mistaken with the solution: we should use a DP instead.

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

You can solve it using DP. We trying to construct a palindrome, but we didn't know it's length. We know some prefix, and we interested only in it's last 7 characters, reversed prefix is suffix because it's palindrome. Each time we trying to add some string into prefix or into suffix. this string must be first unused (or last). we have to remember for each side (prefix and suffix) where was added string at previous step, to prevent "collisions" — situations when two strings coincide. State of dp is 7 last characters of prefix (it's reverse is prefix of suffix), index of first unused string, index of last unused string, and two additinal boolean parameters). Also, don't forget about situation when string placed into the middle of the palidrome, you can try to combine suffix and prefix, after described DP. parameter "7 last characters" can be only one of string from input, or it's reverse.
UPD: Described idea has complexity O(N^3), maybe we should use Dijkstra, to increase perfomance
UPD2: I was wrong, actually it's complexity is O(N^2) because for each (beginStr, lastStr) there is constant number of possible strings to be prefix.
UPD3: my solution

  • »
    »
    12 лет назад, # ^ |
      Проголосовать: нравится +2 Проголосовать: не нравится

    what's cL and cR in your code

    • »
      »
      »
      12 лет назад, # ^ |
        Проголосовать: нравится +2 Проголосовать: не нравится

      "we have to remember for each side (prefix and suffix) where was added string at previous step, to prevent “collisions” — situations when two strings coincide" cL == 1 iff there was string at very left of prefix (s), so we cannot add next string at pos = 0 (i = 0).

      • »
        »
        »
        »
        12 лет назад, # ^ |
          Проголосовать: нравится +2 Проголосовать: не нравится

        thank you , well I almost understand the solution except the "cL",I can't think it clearly ,can you show me an example about the use of cL ,maybe an example is helpful for me to understand ,thanks again

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

          look at second sample — "abcdefg abcdefg".
          L = 0, R = 2, s = "0123456"
          at first step add abcdefg to the prefix at i=7.
          L = 1, R = 2, s = "abcdefg"
          you can't add next "abcdefg" at i=0, and go to L = 2, R = 2 because in this case answer will be abcdefgfedcba (both strings starts at the same place(at very begin of the string), but k[i] must be strictly less than k[i + 1].
          another sample "abcdefg gfedcba" now you CAN add first string to the left, and then second string to the right. anwser abcdefgfedcba.

        • »
          »
          »
          »
          »
          12 лет назад, # ^ |
            Проголосовать: нравится +5 Проголосовать: не нравится

           prefix and it's reverse marked bold
          string added at previous step underlined

          • »
            »
            »
            »
            »
            »
            12 лет назад, # ^ |
              Проголосовать: нравится +5 Проголосовать: не нравится

            I still can't figure it out that why do you write (cR && i==0) its' the only question I have now

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

              i don't know why name of variable is "cL", but you can read it as "can't to put string at very left". if cL == 1, you can't. if cL == 0 you can. look at picture again.
              line 2: L = 1, R = 5, cL = 1, cR = 0
              line 3: L = 1, R = 4, cL = 1, cR = 1
              when we add string "bbbbaaa" to the right side at pos = 0 (i = 0), we should remember we still can't place string at left side. i.e. cL && i == 0.