github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/lcs/lcs.go (about)

     1  package lcs
     2  
     3  // Basic returns the length of the largest common subsequence
     4  func Basic(a, b []int64) int {
     5  	if len(a) == 0 || len(b) == 0 {
     6  		return 0
     7  	}
     8  
     9  	curr := make([]int, len(b))
    10  	prev := make([]int, len(b))
    11  
    12  	max := 0
    13  
    14  	for ai, ax := range a {
    15  		for bi, bx := range b {
    16  			if ax != bx {
    17  				curr[bi] = 0
    18  			} else {
    19  				if ai == 0 || bi == 0 {
    20  					curr[bi] = 1
    21  				} else {
    22  					curr[bi] = 1 + prev[bi-1]
    23  				}
    24  
    25  				if max < curr[bi] {
    26  					max = curr[bi]
    27  				}
    28  			}
    29  		}
    30  		curr, prev = prev, curr
    31  	}
    32  
    33  	return max
    34  }
    35  
    36  // Lift returns the length of the largest common subsequence
    37  func Lift(a, b []int64) int {
    38  	if len(a) == 0 || len(b) == 0 {
    39  		return 0
    40  	}
    41  
    42  	curr := make([]int, len(b))
    43  	prev := make([]int, len(b))
    44  
    45  	max := 0
    46  	for ai, ax := range a {
    47  		if b[ai] == ax {
    48  			prev[ai] = 1
    49  		}
    50  	}
    51  
    52  	for ai := 1; ai < len(a); ai++ {
    53  		ax := a[ai]
    54  
    55  		if ax == b[0] {
    56  			curr[0] = 1
    57  		} else {
    58  			curr[0] = 0
    59  		}
    60  
    61  		for bi := 1; bi < len(b); bi++ {
    62  			bx := b[bi]
    63  			if ax != bx {
    64  				curr[bi] = 0
    65  			} else {
    66  				curr[bi] = 1 + prev[bi-1]
    67  				if max < curr[bi] {
    68  					max = curr[bi]
    69  				}
    70  			}
    71  		}
    72  		curr, prev = prev, curr
    73  	}
    74  
    75  	return max
    76  }
    77  
    78  // Wave returns the length of the largest common subsequence
    79  func Wave(a, b []int64) int {
    80  	if len(a) == 0 || len(b) == 0 {
    81  		return 0
    82  	}
    83  
    84  	prev := make([]int, len(b))
    85  	mid := make([]int, len(b))
    86  	curr := make([]int, len(b))
    87  
    88  	max := 0
    89  	//         prev
    90  	//        / mid
    91  	//       / / curr
    92  	//      / / /
    93  	// 0 1 2 3 4
    94  	// 0 1 2 3
    95  	// 0 1 2
    96  	// 0 1
    97  	// 0
    98  
    99  	for wave := 0; wave < len(b); wave++ {
   100  		for i := 0; i < wave; i++ {
   101  			ax, bx := a[wave], b[wave-i]
   102  			if ax == bx {
   103  				if i == 0 {
   104  					curr[i] = 1
   105  				} else {
   106  					curr[i] = prev[i-1] + 1
   107  					if curr[i] > max {
   108  						max = curr[i]
   109  					}
   110  				}
   111  			} else {
   112  				curr[i] = 0
   113  			}
   114  		}
   115  		prev, mid, curr = mid, curr, prev
   116  	}
   117  
   118  	//           prev
   119  	//          / mid
   120  	//         / / curr
   121  	//        / / /
   122  	//       3 4 .
   123  	//     2 3 3
   124  	//   1 2 2 2
   125  	// 0 1 1 1 1
   126  	// 0 0 0 0 0
   127  
   128  	for wave := len(b) - 2; wave >= 0; wave-- {
   129  		for i := 0; i < wave; i++ {
   130  			ax, bx := a[wave], b[wave-i]
   131  			if ax == bx {
   132  				if i == 0 {
   133  					curr[i] = 1
   134  				} else {
   135  					curr[i] = prev[i-1] + 1
   136  					if curr[i] > max {
   137  						max = curr[i]
   138  					}
   139  				}
   140  			} else {
   141  				curr[i] = 0
   142  			}
   143  		}
   144  		prev, mid, curr = mid, curr, prev
   145  	}
   146  
   147  	return max
   148  }