Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 47
Question meaning:
Give the correct time table for 1-N events and the time table for 1-N events to calculate the maximum number of events, which is the same as the correct order.
Solution:
Sort Events in order of ranking, and then find the length of the longest common subsequence.
DP [I] [J] = DP [I-1] [J-1] + 1 (save1 [I] = save [J]) DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]);
Code:
# Include <iostream>
# Include <cmath>
# Include <cstdio>
# Include <cstdlib>
# Include <string>
# Include <cstring>
# Include <algorithm>
# Include <vector>
# Include <map>
# Include <stack>
# Include <queue>
# Define EPS 1e-6
# Define Inf (1 <20)
# Define PI ACOs (-1.0)
Using namespace STD;
Int save1 [30], save2 [30];
Int DP [30] [30];
Int main ()
{
Int N, temp;
Scanf ("% d", & N );
For (INT I = 1; I <= N; I ++)
{
Scanf ("% d", & temp );
Save1 [temp] = I; // sort events in the order of ranking, so that the longest common subsequence has a relatively same ranking
}
While (scanf ("% d", & temp )! = EOF)
{
Save2 [temp] = 1;
For (INT I = 2; I <= N; I ++)
{
Scanf ("% d", & temp );
Save2 [temp] = I;
}
Memset (DP, 0, sizeof (DP ));
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= N; j ++)
{
If (save1 [I] = save2 [J])
DP [I] [J] = DP [I-1] [J-1] + 1; // max (DP [I-1] [J-1] + 1, DP [I-1] [J], DP [I] [J-1]);
Else
DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]);
}
Printf ("% d \ n", DP [N] [N]);
}
Return 0;
}