-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11051.cpp
106 lines (91 loc) · 1.27 KB
/
11051.cpp
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
103
104
105
106
//11051
#include<iostream>
#include<unordered_map>
#include<string>
#include<vector>
using namespace std;
struct LinkNode
{
string value = "";
LinkNode* next = nullptr;
};
void Clear(LinkNode* head)
{
LinkNode* temp;
while (head->next)
{
temp = head->next;
head->next = temp->next;
delete temp;
}
delete head;
}
int main()
{
int n;
string num;
cin >> n;
unordered_map<string, int> hash;
for (int i = 0; i < n; i++)
{
cin >> num;
if (hash[num])
{
continue;
}
hash[num] = i + 1;
}
int m;
int count = 0;
int index = 0;
LinkNode *head, *p, *q;
head = new LinkNode;
q = head;
cin >> m;
for (int j = 0; j < m; j++)
{
cin >> num;
if (hash[num]) {
index = hash[num];
}
else {
count += n;
continue;
}
if (index < 0)
{
q = head;
count += 1;
while (q->next->value != num)
{
count += 1;
q = q->next;
}
p = q->next;
q->next = p->next;
p->next = head->next;
head->next = p;
}
else
{
q = head;
while (q->next)
{
q = q->next;
if (abs(hash[q->value]) > index)
{
count += 1;
}
}
count += index;
p = new LinkNode;
p->value = num;
p->next = head->next;
head->next = p;
hash[num] = -index;
}
}
Clear(head);
cout << count << endl;
return 0;
}