|
| 1 | +function max(a,b) { |
| 2 | + if(a>b){ |
| 3 | + return a; |
| 4 | + } else { |
| 5 | + return b; |
| 6 | + } |
| 7 | +} |
| 8 | +logger._print("LPS for any string with length = 1 is 1"); |
| 9 | +for(i=2;i<=N;i++) { |
| 10 | + logger._print("--------------------------------------------------"); |
| 11 | + logger._print("Considering a sub-string of length "+i); |
| 12 | + logger._print("--------------------------------------------------"); |
| 13 | + for(j=0;j<N-i+1;j++) { |
| 14 | + var k = j+i-1; |
| 15 | + tracer._select(j)._wait(); |
| 16 | + tracer._notify(k)._wait(); |
| 17 | + |
| 18 | + logger._print("Comparing "+seq[j] + " and "+seq[k]); |
| 19 | + |
| 20 | + if(seq[j]==seq[k] && i==2) { |
| 21 | + logger._print("They are equal and size of the string in the interval"+j+" to "+k+" is 2, so the Longest Palindromic Subsequence in the Given range is 2"); |
| 22 | + |
| 23 | + matrix._notify(j,k)._wait(); |
| 24 | + |
| 25 | + L[j][k]=2; |
| 26 | + matrix._setData(L); |
| 27 | + |
| 28 | + matrix._denotify(j,k)._wait(); |
| 29 | + |
| 30 | + } else if(seq[j]==seq[k]) { |
| 31 | + logger._print("They are equal, so the Longest Palindromic Subsequence in the Given range is 2 + the Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k-1)); |
| 32 | + |
| 33 | + matrix._notify(j,k)._wait(); |
| 34 | + matrix._select(j+1,k-1)._wait(); |
| 35 | + |
| 36 | + L[j][k] = L[j+1][k-1] + 2; |
| 37 | + matrix._setData(L); |
| 38 | + |
| 39 | + matrix._denotify(j,k)._wait(); |
| 40 | + matrix._deselect(j+1,k-1)._wait(); |
| 41 | + |
| 42 | + } else { |
| 43 | + logger._print("They are NOT equal, so the Longest Palindromic Subsequence in the Given range is the maximum Longest Increasing Subsequence between the indices "+(j+1)+" to "+(k) + " and "+(j)+" to "+(k-1)); |
| 44 | + matrix._notify(j,k)._wait(); |
| 45 | + matrix._select(j+1,k)._wait(); |
| 46 | + matrix._select(j,k-1)._wait(); |
| 47 | + |
| 48 | + L[j][k] = max(L[j+1][k],L[j][k-1]); |
| 49 | + matrix._setData(L); |
| 50 | + |
| 51 | + matrix._denotify(j,k)._wait(); |
| 52 | + matrix._deselect(j+1,k)._wait(); |
| 53 | + matrix._deselect(j,k-1)._wait(); |
| 54 | + } |
| 55 | + logger._print("--------------------------------------------------"); |
| 56 | + tracer._deselect(j)._wait(); |
| 57 | + tracer._denotify(k)._wait(); |
| 58 | + } |
| 59 | +} |
| 60 | +logger._print("Longest Increasing Subsequence of the given string = L[0]["+(N-1)+"]="+L[0][N-1]); |
0 commit comments