forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva-111.cpp
More file actions
102 lines (76 loc) · 1.55 KB
/
uva-111.cpp
File metadata and controls
102 lines (76 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//uva 111
//History Grading
#include <iostream>
#include <vector>
using namespace std;
int LCS(vector <int> & a, vector <int> & b);
int find(vector <int> & num, int a);
int max(int a, int b);
int main(void)
{
int n = 0;
vector <int> scores;
vector <int> correctOrder;
while(true) {
char c = ' ';
bool end = 0;
while (true) {
int t = 0;
if (scanf("%d%c", &t, &c) == EOF){
end = 1;
break;
}
scores.push_back(t);
if (c == '\n')
break;
}
if (scores.size() == 1){
n = scores[0];
correctOrder.clear();
for (int i = 0; i < n; ++i){
int t;
cin >> t;
correctOrder.push_back(t);
}
getchar();//removing \n
}
else if (scores.size() == n){
vector <int> correctOrderRank(n);
vector <int> scoresRank(n);
for (int i = 0; i < n; ++i){
correctOrderRank[correctOrder[i] - 1] = i + 1;
scoresRank[scores[i] - 1] = i + 1;
}
int result = LCS(correctOrderRank, scoresRank);
cout << result << endl;
}
scores.clear();
if (end)
break;
}
return 0;
}
int LCS(vector <int> & a, vector <int> & b)
{
int m = a.size() + 1;
int n = b.size() + 1;
vector < vector <int> > arr(m, vector <int> (n));
for(int i = 1; i < m; ++i)
for (int j = 1; j < n; ++j)
if (a[i - 1] == b[j - 1])
arr[i][j] = arr[i - 1][j - 1] + 1;
else
arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
return arr[m - 1][n - 1];
}
int max(int a, int b)
{
return a > b ? a : b;
}
int find(vector <int> & num, int a)
{
for (int i = 0; i < num.size(); ++i)
if (num[i] == a)
return i;
return -1;
}