Skip to main content

Light OJ 1110 - An Easy LCS

LCS:  An Easy LCS



This is an interesting LCS dp for problem for beginner. There is many solution for this problem. In problem this firstly we calculate the size of LCS using recursively.

  1. string A, B;
  2. int n, m;
  3. int dp[102][102];
  4. int lcs(int i, int j)
  5. {
  6.     if (== n + 1 || j == m + 1) {
  7.         return 0;
  8.     }
  9.     if (dp[i][j] != -1) return dp[i][j];
  10.     int x = 0;
  11.     if (A[i] == B[j]) {
  12.         x = lcs(+ 1, j + 1) + 1;
  13.     }
  14.     else {
  15.         x = max(lcs(+ 1, j), lcs(i, j + 1));
  16.     }
  17.     dp[i][j] = x;
  18.     return x;
  19. }
Then we start to use a to z for getting lexicographically smallest LCS. First we try to use a whether a is the part of LCS and then b and so on. For more clear to you code is given.
  1. for (int i = 0; i < k; i++) {
  2.             for (int j = 'a'; j <= 'z'; j++) {
  3.                 ck = 0;
  4.                 x = a;
  5.                 while (A[x]) {
  6.                     if (A[x] == j) {
  7.                         ck++;
  8.                         break;
  9.                     }
  10.                     x++;
  11.                 }
  12.                 y = b;
  13.                 while (B[y]) {
  14.                     if (B[y] == j) {
  15.                         ck++;
  16.                         break;
  17.                     }
  18.                     y++;
  19.                 }
  20.                 if (ck == 2) {
  21.                     if (dp[a][b] == dp[x][y]) {
  22.                         res += A[x];
  23.                         a = x + 1;
  24.                         b = y + 1;
  25.                         break;
  26.                     }
  27.                 }
  28.             }
  29.         }


Comments

Popular posts from this blog

Interview Topics

Object-Oriented Programming  1. GeeksforGeeks                                         2.OPPs interview Java Concept 1. Basic Principles                                      2. Memory Management          3. Interview Questions                            4. SOLID 5. CppNuts                                                 6.Java Guides*** Networking 1. Java Socket                                           2.Kunal Kushwaha Layer, TCP, UDP Operating System   1.Farhan Hossan ...

Resources for Programming Contest

Some important resources:                                           BAPS-BACS 1.CPPS                                                           7.Problem Solving Strategies 2.Beginner to Expert                                     8.An Awesome Blog for Programming 3.Bangla Resources                                      9.Ahnaf's Bangla Blog 4.Collection of Shakkhar                              10. Collection of UpsideDown ...

Web Development

Basic for Front End and Backend 1.JavaScriptInfo                                                                         2.Sumit 3.Apna College***                                                                          4. W3School 5.Mosh