/
main.cpp
151 lines (148 loc) · 6.15 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <bits/stdc++.h>
using namespace std;
#include "Parameter.h"
#include "String.h"
#include "Expression.h"
#include "Keyword.h"
#include "Mail.h"
#include "Heap.h"
#include "priority.h"
#include "Order_Tree.h"
#include "statistic.h"
Mail mail[MAX_MAIL];
Heap<MAX_MAIL, int> longest;
Priority priority;
Statistics statistic;
Order_Tree order_tree;
int mail_cnt = 0, mail_place[MAX_MAIL];
String<50> mail_file[MAX_MAIL];
unordered_map<String<50>, int> record; // record the file named for escaping adding a file repeatly
int main() {
memset(mail_place, -1, sizeof mail_place);
String<50> file_string;
unordered_set<String<50>> file;
int from_id, to_id;
long long datel, dater;
char c, *p;
priority.init();
while (scanf("%s", temp_string) != EOF) {
switch (temp_string[0]) { // switch is more faster
case 'a':
assert(scanf("%s", temp_string) != EOF);
file_string.input_temp(temp_string);
if (file.find(file_string) == file.end()) {
if (record.find(file_string) != record.end()) {
int x = record[file_string];
mail_place[mail[x].id] = x;
longest.push(mail[x].length, mail[x].id);
file.insert(mail_file[mail[x].id]);
order_tree.insert(mail[x]);
priority.insert(mail[x]);
statistic.add_statistics(mail[x]);
}
else {
file.insert(file_string);
FILE *f = fopen(temp_string, "r");
mail[mail_cnt].input(f);
mail_place[mail[mail_cnt].id] = mail_cnt;
mail_file[mail[mail_cnt].id] = file_string;
longest.push(mail[mail_cnt].length, mail[mail_cnt].id);
fclose(f);
order_tree.insert(mail[mail_cnt]);
priority.insert(mail[mail_cnt]);
statistic.add_statistics(mail[mail_cnt]);
record[file_string] = mail_cnt++;
}
printf("%d\n", int(file.size()));
}
else
puts("-");
break;
case 'r':
int x;
assert(scanf("%d", &x));
if (!~mail_place[x])
puts("-");
else {
order_tree.erase(mail[mail_place[x]]);
priority.erase(x);
longest.erase(x);
statistic.remove_statistics(mail[mail_place[x]]);
mail_place[x] = -1;
file.erase(mail_file[x]);
printf("%d\n", int(file.size()));
}
break;
case 'l':
if (file.empty())
puts("-");
else
printf("%d %d\n", longest.top(), mail[mail_place[longest.top()]].length);
break;
case 'q':
from_id = to_id = -1;
datel = 0, dater = 1e18;
while (scanf("%s", temp_string) && temp_string[0] == '-') {
switch (temp_string[1]) {
case 'f':
for (int i = 3; temp_string[i]; ++i)
if (temp_string[i] >= 'A' && temp_string[i] <= 'Z')
temp_string[i] ^= 'a' ^ 'A';
else if (temp_string[i] == '"')
temp_string[i] = 0;
from_id = user_hash(temp_string + 3);
break;
case 't':
for (int i = 3; temp_string[i]; ++i)
if (temp_string[i] >= 'A' && temp_string[i] <= 'Z')
temp_string[i] ^= 'a' ^ 'A';
else if (temp_string[i] == '"')
temp_string[i] = 0;
to_id = user_hash(temp_string + 3);
break;
case 'd':
p = temp_string + 2;
while ((c = *p++) != '~')
datel *= 10, datel += c ^ '0';
if (*p)
for (dater = 0; (c = *p++);)
dater *= 10, dater += c ^ '0';
break;
}
}
{
for (int i = 0; temp_string[i]; ++i)
if (temp_string[i] >= 'A' && temp_string[i] <= 'Z')
temp_string[i] ^= 'a' ^ 'A';
Expression exp;
exp.input(temp_string);
order_tree.query(from_id, to_id, datel, dater, exp);
}
break;
case 'p':
if (temp_string[1] == 'r') {
int k;
assert(scanf("%d", &k) != EOF);
auto v = priority.query(k);
if (v.empty())
puts("-");
else
for (int i = 0; i < int(v.size()); ++i)
printf("%d%c", v[i], " \n"[i + 1 == int(v.size())]);
if (int(v.size()) < k)
printf("The number of mail is less than %d...\n", k);
}
else {
int m, n;
assert(scanf("%s%d%d", temp_string, &m, &n) != EOF);
for (int i = 1; temp_string[i]; ++i)
if (temp_string[i] >= 'A' && temp_string[i] <= 'Z')
temp_string[i] ^= 'a' ^ 'A';
else if (temp_string[i] == '"')
temp_string[i] = 0;
from_id = user_hash(temp_string + 1);
statistic.peek(from_id, m, n);
}
}
}
}