I just learned this technique and it is very good. It can be used directly as a template. It is estimated that there will be no such troubles as variants. Directly paste the code. It's too short. Remember it at first glance.
[Cpp]
# Include <iostream>
Using namespace std;
Int a [505], B [505], f [505];
Int main ()
{
Int T, I, n1, n2, j, k, max;
Cin> T;
For (I = 1; I <= T; I ++)
{
Cin> n1;
For (j = 1; j <= n1; j ++)
Cin> a [j];
Cin> n2;
For (j = 1; j <= n2; j ++)
Cin> B [j];
Memset (f, 0, sizeof (f ));
For (j = 1; j <= n1; j ++)
{
Max = 0;
For (k = 1; k <= n2; k ++)
{
If (a [j]> B [k] & max <f [k])
Max = f [k];
If (a [j] = B [k])
F [k] = max + 1;
}
}
Max = 0;
For (j = 1; j <= n2; j ++)
If (f [j]> max)
Max = f [j];
Cout <max <endl;
If (I! = T)
Cout <endl;
}
Return 0;
}
# Include <iostream>
Using namespace std;
Int a [505], B [505], f [505];
Int main ()
{
Int T, I, n1, n2, j, k, max;
Cin> T;
For (I = 1; I <= T; I ++)
{
Cin> n1;
For (j = 1; j <= n1; j ++)
Cin> a [j];
Cin> n2;
For (j = 1; j <= n2; j ++)
Cin> B [j];
Memset (f, 0, sizeof (f ));
For (j = 1; j <= n1; j ++)
{
Max = 0;
For (k = 1; k <= n2; k ++)
{
If (a [j]> B [k] & max <f [k])
Max = f [k];
If (a [j] = B [k])
F [k] = max + 1;
}
}
Max = 0;
For (j = 1; j <= n2; j ++)
If (f [j]> max)
Max = f [j];
Cout <max <endl;
If (I! = T)
Cout <endl;
}
Return 0;
}