Home c++ the largest overall growing sequence

the largest overall growing sequence

Author

Date

Category

There is a task. Find the largest overall growing sequence among two sequences long n and m. I wrote an algorithm for O (n ^ 2), but I was told that there is a solution for O (n). Waiting for help! If you specify the direction of movement, it will also be good. The algorithm must be implemented on C++ in the form of F – and.


Answer 1, Authority 100%

More common Task finding the highest total subsequence (for the case of two sequences) is solved by dynamic programming for O (n * m).

related Task finding the highest total substring in two lines (alphabet bounded) is solved by suffix trees for O (n + m).

I think the truth is somewhere nearby). In theory, it is necessary to use the subsequence ascending condition.

Also worth looking at suffix trees.


Answer 2, Authority 67%

It seems to me that there was a mistake or inaccuracy of the wording somewhere here. Even the task of finding the greatest increasing subsequence of one given sequence It is solved in the general case for O (n log n), and here it is required for O (n + m) find the largest total subsequence for
Sequence pairs?

Maybe they meant the substring? If the initial sequences increase, the solution has already been given. If not, then you can divide them to growing areas (the answer cannot cross the border of such sites) and try to achieve the desired asymptotics (I do not know whether it will work). You can also really apply suffix trees, more precisely, This algorithm. It seems to be easy to modify for an increasing substring, going only by increasing paths in the tree.


Answer 3, Authority 33%

I managed to solve only for O (n ^ 2). I think it will not work much faster.
Here is the code

int comlen (char * p, char * q) {
  INT MAXLEN = 0;
  While ((* P! = '\ 0' || * Q! = '\ 0') & amp; & amp; * p ++ == * Q ++)
     ++ maxlen;
  RETURN MAXLEN;
}
Int Isinc (Char * P, Int Len) {
  int i = 1;
  int k = 0;
  For (; * p! = '\ 0' & amp; & amp; i & lt; len; ++ i, ++ k) {
     IF (p [i] & lt; p [k])
     Return 0;
   }
   Return 1;
}
INT LCIS (Char * Str1, Char * Str2, Int Len) {
  int maxlen = -1;
  int thislen = -1;
  int maxi = 0, maxj = 0;
  for (int i = 0; i & lt; len; ++ i) {
    For (int j = 0; j & lt; len; ++ j) {
      Thislen = Comlen (& amp; STR1 [i], & amp; STR2 [J]);
        if (ISINC (& amp; STR1 [I], thisLEN)) {
          if (thislen & gt; maxlen) {
            Maxlen = thislen;
            maxi = i;
            maxj = j;
          }
        }
     }
  }
  RETURN MAXLEN;
}

Answer 4

There is an algorithm 0 (n ~ M). Something between N and m.
The meaning is:

  • There are global iterators (or pointers) on:
    • Begin in the first sequence, originally = 0
    • Start in the second sequence, originally = 0
    • sequence length, originally = 0
  • Current Variables
  • Begin in the first sequence, originally = 0 // always equal to 0, if we are not in the total subsequence
  • Start in the second sequence, originally = 0 // always equal to 0, if we are not in the total subsequence
  • long sequence, originally = 0
  • There are some current pointers and a cycle condition

  For (Iterator i1 = listn.begin (), iterator i2 = listm.begin (); i1! = Listn.end () & amp; & amp; i2! = Listm.end ();)
  {
    if (we are not in the total subsequence)
    {
     if (* Il = * i2)
     {
       then we start a new subsidence, at the same time the length of the current subsequence ++;
       ++ I1;
       ++ I2;
     }
     ELSE.
     {
       We shift that iterator, the value for which is less;
     }
    }
    ELSE if (we are in general subsequence)
    {
     if (* Il = * i2)
     {
       The length of the current subsequence ++;
       ++ I1;
       ++ I2;
     }
     ELSE.
     {
       The total subsequence is over
       Compare it with the found global;
       If the current is more global, equate the global current;
       We put the current starts of the sequence in NULL,
       and the current length of subsequence in 0;
       We shift that iterator, the value for which is less;
     }
    }
  }

After the passage of the cycle in global variables will be a result: if the subsequence was,
It is derived either both iterator if the values ​​are equal or 1 of them, by which the value is greater, until the end of one of the lists is reached.


Answer 5

I propose to pay tribute to this article: The longest increasing subsequence for O (n log n)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions