Can I print the LCS using top down approach of dynamic programming? - dynamic-programming

I know there is a solution using the bottom-up approach. But I couldn't find any solution using top-down approach anywhere. Here is the code I'm using for finding the number of characters in LCS:
#include <bits/stdc++.h>
using namespace std;
int memo[100][100];
int LCS(string a,string b,int x,int y){
if(x==a.size() || y==b.size())
{ memo[x][y]=0;
return 0;
}
if(a[x]==b[y])
{
if(memo[x][y]==-1){
memo[x][y]=LCS(a,b,x+1,y+1);
}
return 1+ memo[x][y];
}
else
{
if(memo[x][y]==-1)
memo[x][y]=max(LCS(a,b,x+1,y),LCS(a,b,x,y+1));
return memo[x][y];
}
}
int main(int argc, char const *argv[])
{
string a,b;
cin>>a>>b;
memset(memo,-1,sizeof(memo));
cout<<LCS(a,b,0,0)<<endl;
return 0;
}

You need to use your memo while you compare both strings to do it. If they are equal, you jump at the diagonal, and if the are different, you can look for up and left jumping to the major one.
I have a code similar to yours. The only difference is that I look from the end to the begin of the string:
int lcs(char *x, char *y, int px, int py)
{
int ans;
int m1, m2;
if (memo[px][py] > -1) {
return memo[px][py];
}
if ((px == 0) || (py == 0)) {
ans = 0;
} else if (x[px - 1] == y[py - 1]) {
ans = lcs(x, y, px - 1, py - 1) + 1;
} else {
m1 = lcs(x, y, px , py - 1);
m2 = lcs(x, y, px - 1, py );
//max (m1, m2)
if (m1 > m2) {
ans = m1;
} else {
ans = m2;
}
}
memo[px][py] = ans;
return ans;
}
To call lcs, you can do lcs("marvin", "panic", strlen("marvin"), strlen("panic")).
So, my memo (that is inverted considering your solution) will generate the following table:
+-------------------------+
| p a n i c |
+-------------------------+
| | -1 0 0 0 0 0 |
| m | 0 0 0 0 0 0 |
| a | 0 0 1 1 1 1 |
| r | 0 0 1 1 1 1 |
| v | 0 0 1 1 1 1 |
| i | 0 0 1 -1 2 2 |
| n | -1 -1 -1 2 2 2 |
+-------------------------+
To recover the substring, we start at the end of both strings ([6, 5]) and look for equal characters. If they aren't equals, we look to the content of the table in the up position. If this position is bigger than left, we go to up, otherwise we go to the left. This is represented for the following code:
px = strlen("marvin");
py = strlen("panic");
pos = 0;
while ((px != 0) && (py != 0)) {
if (x[px - 1] == y[py - 1]) {
res[pos++] = x[px - 1];
px--;
py--;
} else if (memo[px - 1][py] > memo[px][py -1]) {
px--;
} else {
py--;
}
}
res[pos] = '\0';
printf("%s\n", strrev(res));
This piece of code will start at the end, and will go left til it finds the the char n (location F).
+-------------------------+
| p a n i c |
+-------------------------+
| | -1 0 0 0 0 0 |
| m | 0 0 0 0 0 0 |
| a | 0 0 1 1 1 1 |
| r | 0 0 1 1 1 1 |
| v | 0 0 1 1 1 1 |
| i | 0 0 P -1 2 2 |
| n | -1 -1 -1 F - - |
+-------------------------+
When it finds the char n the algorithm position jumps at the superior-left-diagonal (location P). In the next steps, the algorithm go up until matches with the char a.
+-------------------------+
| p a n i c |
+-------------------------+
| | -1 0 0 0 0 0 |
| m | 0 P 0 0 0 0 |
| a | 0 0 F 1 1 1 |
| r | 0 0 | 1 1 1 |
| v | 0 0 | 1 1 1 |
| i | 0 0 | -1 2 2 |
| n | -1 -1 -1 \ - - |
+-------------------------+
And the last steps it will go left until the position of px reach zero and the algorithm stops.
+-------------------------+
| p a n i c |
+-------------------------+
| | -1 0 0 0 0 0 |
| m | - - 0 0 0 0 |
| a | 0 0 \ 1 1 1 |
| r | 0 0 | 1 1 1 |
| v | 0 0 | 1 1 1 |
| i | 0 0 | -1 2 2 |
| n | -1 -1 -1 \ - - |
+-------------------------+
Obs: The result will be na and needs to be reverted (an).

Related

Counting value occurrence in string variable

In this example, how could I know how many 0, 1 and 2 there are in each variable?
It looks like you want to count the number of occurrences of each digit in each observation.
You can do this as follows:
clear
input str5 string
"22112"
"21012"
"22012"
"22022"
"21122"
"21112"
"21002"
"...0."
"...0."
"20002"
"..00."
"2..01"
"22212"
"21022"
"12212"
end
generate x0 = length(string) - length(subinstr(string, "0", "", .))
generate x1 = length(string) - length(subinstr(string, "1", "", .))
generate x2 = length(string) - length(subinstr(string, "2", "", .))
The idea here is to calculate the difference in the length of the string after you eliminate every instance of the digit of interest.
The above code snippet will produce the desired output:
list
+-----------------------+
| string x0 x1 x2 |
|-----------------------|
1. | 22112 0 2 3 |
2. | 21012 1 2 2 |
3. | 22012 1 1 3 |
4. | 22022 1 0 4 |
5. | 21122 0 2 3 |
|-----------------------|
6. | 21112 0 3 2 |
7. | 21002 2 1 2 |
8. | ...0. 1 0 0 |
9. | ...0. 1 0 0 |
10. | 20002 3 0 2 |
|-----------------------|
11. | ..00. 2 0 0 |
12. | 2..01 1 1 1 |
13. | 22212 0 1 4 |
14. | 21022 1 1 3 |
15. | 12212 0 2 3 |
+-----------------------+

Creating a Results Grid from a Pandas DataFrame

I have the following table of results in a Pandas DataFrame. Each player has been assigned an ID number:
+----------------+----------------+-------------+-------------+
| Home Player ID | Away Player ID | Home Points | Away Points |
+----------------+----------------+-------------+-------------+
| 1 | 2 | 3 | 0 |
| 3 | 4 | 1 | 1 |
| 2 | 3 | 3 | 0 |
| 4 | 1 | 3 | 0 |
| 2 | 4 | 1 | 1 |
| 3 | 1 | 1 | 1 |
| 2 | 1 | 0 | 3 |
| 4 | 3 | 1 | 1 |
| 3 | 2 | 0 | 3 |
| 1 | 4 | 0 | 3 |
| 4 | 2 | 1 | 1 |
| 1 | 3 | 1 | 1 |
+----------------+----------------+-------------+-------------+
The aim is to create a 4x4 numpy matrix (dimensions equal to the number of players) and fill the matrix with the points they earned from games between the respective players.
The matrix should end up like this:
+--------+---+---+---+---+
| Matrix | 1 | 2 | 3 | 4 |
+--------+---+---+---+---+
| 1 | 0 | 3 | 1 | 0 |
| 2 | 0 | 0 | 3 | 1 |
| 3 | 1 | 0 | 0 | 1 |
| 4 | 3 | 1 | 1 | 0 |
+--------+---+---+---+---+
The left hand column is the ID number of the home players, with the column headers the IDs of the away players.
For example, when the Home Player ID = 1 and the Away Player ID = 2, Player 1 earned 3 points, so the entry for the Matrix(1,2) (or 0,1 because of the zero indexing) would equal 3.
I can just about manage to do this with two for loops, but it seems quite inefficient. Is there a better way to achieve this?
Would really appreciate any advice!
Use
In [217]: df.pivot_table(columns='Home Player ID', index='Away Player ID',
values='Away Points', fill_value=0)
Out[217]:
Home Player ID 1 2 3 4
Away Player ID
1 0 3 1 0
2 0 0 3 1
3 1 0 0 1
4 3 1 1 0
Or use
In [221]: df.set_index(['Away Player ID', 'Home Player ID'])['Away Points'].unstack(fill_value=0)
Out[221]:
Home Player ID 1 2 3 4
Away Player ID
1 0 3 1 0
2 0 0 3 1
3 1 0 0 1
4 3 1 1 0

Boolean operations for ITE-Algorithm

I'm a bit confused. I need to make an example of using the ITE-Algorithm on every Boolean operation. But I actually don't know how much there are?
As an example I mean, AND, OR, XOR, XNOR, NOR, NAND, NOT. And I don't know the other ones... I at least miss one because it cannot be seven operations. (2^n)
I need to make an example of using the ITE-Algorithm on every Boolean
operation.
This is Exercise 1 from the Fascicle 1B of the Volume 4 of Knuth's The art of computer programming.
The answer is the following.
But I actually don't know how much there are?
There are exactly 16 boolean functions of two variables (and 22ⁿ functions of n variables). Just permutate rows in functions' truth tables.
| F0 | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | FA | FB | FC | FD | FE | FH |
|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
F0 and FH do not depend on the values of both variables.
F3, F5, FA, FC depend on the value of only one variable.
The table below lists common names of these functions.
The above table is from the Fascicle 0B of the Volume 4.
I'd add that F8 and FE are also known as Pierce's arrow and Sheffer stroke respectively.

How to interpolate zero cells in a large excel sheet?

I have a long excel file which contains numbers collected from a website. Less than 1% of the cells contain zero due to an error from the source. Therefore, I want to find update those cells and interpolate them with the nearest values. The length of the zero cells is some time single, hence I can simply take the average of the nearest non-zero values. However, a few places it is longer than one, hence I need to use linear interpolation.
Sample extracted data
+---+------+------+------+------+------+------+------+------+---+---+---+---+---+---+---+---+------+------+------+------+------+------+------+------+------+
| | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y |
+---+------+------+------+------+------+------+------+------+---+---+---+---+---+---+---+---+------+------+------+------+------+------+------+------+------+
| 1 | 4058 | 4048 | 4049 | 4082 | 4090 | 4115 | 4118 | 4109 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3990 | 4058 | 4064 | 4053 | 4057 | 4093 | 4123 | 4137 | 4133 |
+---+------+------+------+------+------+------+------+------+---+---+---+---+---+---+---+---+------+------+------+------+------+------+------+------+------+
Here's a very general script that might do something like that. It is only tested on positive values and ten rows, so you will definitely need to adapt it to many corner cases - but it should point you in the right direction:
Sub Interpolate()
Dim valueToTop As Integer
For Row = 1 To 10
valueToTop = -1
valueToBottom = -1
If Cells(Row, 1).Value = 0 Then
RowToTop = Row - 1
Do While RowToTop > 0
If Cells(RowToTop, 1).Value > 0 Then
valueToTop = Cells(RowToTop, 1)
Exit Do
End If
RowToTop = RowToTop - 1
Loop
Debug.Print valueToTop
Debug.Print RowToTop
RowToBottom = Row + 1
Do While RowToBottom > 0
If Cells(RowToBottom, 1).Value > 0 Then
valueToBottom = Cells(RowToBottom, 1)
Exit Do
End If
RowToBottom = RowToBottom + 1
Loop
Debug.Print valueToBottom
Debug.Print RowToBottom
Cells(Row, 2).Value = valueToTop + (Row - RowToTop) * (valueToBottom - valueToTop) / (RowToBottom - RowToTop)
End If
Next Row
End Sub

How to get the cell with the highest number, work with it, get the next highest and so on in excel?

I'm trying to get a cell with value BBBBBBBGGGGGJJJJCCCCDDDDAA from these cells:
-----------------------------------------
| 2 | 7 | 4 | 4 | 0 | 0 | 5 | 0 | 0 | 4 |
-----------------------------------------
So it gets the highest value and writes the cell's horizontal address (that might have an offset) that many times. Then gets the next highest and does the same thing until it reaches the zeroes. Is that possible in excel?
additional samples:
------------------------------------------------------------------------------------
| 2 | 0 | 0 | 3 | 0 | 0 | 5 | 0 | 0 | 0 | GGGGGDDDAA |
------------------------------------------------------------------------------------
| 0 | 0 | 2 | 0 | 0 | 0 | 5 | 0 | 0 | 0 | GGGGGCC |
------------------------------------------------------------------------------------
| 0 | 7 | 2 | 2 | 4 | 3 | 3 | 0 | 0 | 0 | BBBBBBBEEEEFFFGGGCCDD |
------------------------------------------------------------------------------------
| 4 | 7 | 0 | 7 | 7 | 0 | 0 | 0 | 8 | 7 | IIIIIIIIBBBBBBBDDDDDDDEEEEEEEJJJJJJJAAAA |
------------------------------------------------------------------------------------
| 0 | 2 | 0 | 2 | 8 | 0 | 8 | 0 | 7 | 10| JJJJJJJJJJEEEEEEEEGGGGGGGGIIIIIIIBBDD |
------------------------------------------------------------------------------------

Resources