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

Автор determinism, 9 лет назад, По-английски

I couln't analyse the complexity of this solution to this problem. Can someone explain why even simple recursive approach doesn't get TLE?

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

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

Maybe it's because the maximum length of the string is 25? Though you can solve this problem with dynamic programming in O(N3) with the same recurrence relation using memoization.

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

    There are 2^26-2 = 67108862 states, and what you do in one state is linear. Can you explain why it's O(N^3)?

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

      Let

      Unable to parse markup [type=CF_TEX]

      be a boolean function that tells us if we can reduce the substring

      Unable to parse markup [type=CF_TEX]

      . Also define function

      Unable to parse markup [type=CF_TEX]

      and

      Unable to parse markup [type=CF_TEX]

      , where

      Unable to parse markup [type=CF_TEX]

      the largest index

      Unable to parse markup [type=CF_TEX]

      such that

      Unable to parse markup [type=CF_TEX]

      , and

      Unable to parse markup [type=CF_TEX]

      as the lowest left index with the mentioned property.

      Now here's the code for the recurence:

      dp[i][j]=0;
      if (r[i]>i) dp[i][j]=dp[r[i]+1][j];
      for (k=r[i]+1; k<=j; k++)
          if (s[i]==s[k])
             dp[i][j]=dp[i][j] | (dp[r[i]+1][l[k]-1]] & dp[k+1][j]);
      

      (There are also some cornes cases)

      We observe that if

      Unable to parse markup [type=CF_TEX]

      then we can transition to state

      Unable to parse markup [type=CF_TEX]

      and see if we can reduce the substring

      Unable to parse markup [type=CF_TEX]

      . But also we can combine the substring

      Unable to parse markup [type=CF_TEX]

      with some other substring

      Unable to parse markup [type=CF_TEX]

      if all their characters are equal. So we are left to reduce

      Unable to parse markup [type=CF_TEX]

      and

      Unable to parse markup [type=CF_TEX]

      . We use

      Unable to parse markup [type=CF_TEX]

      and

      Unable to parse markup [type=CF_TEX]

      because it's always better to pick these indeces instead of fixing the right point of an interval and try to extend it to the left until we get to a different character.

      I know that I worded it pretty badly, but try to simulate the recurrence on the examples and it should become clear why this kind of approach works. (but take notice, I'm not 100% sure that this solution is correct)