-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.h
150 lines (135 loc) · 3.12 KB
/
basics.h
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
#ifndef BASICS_H
#define BASICS_H
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
namespace Basics
{
template <typename T>
class ReadWrite
{
public:
static vector<T> readVector()
{
int n;
cin >> n;
vector<T> v(n);
for (int i = 0; i < n; ++i)
cin >> v[i];
return v;
}
static vector<vector<T>> readMatrix()
{
int n, m;
cin >> n >> m;
vector<vector<T>> v(n, vector<T>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> v[i][j];
return v;
}
static void writeVector(vector<T>& v)
{
for (int i = 0; i < v.size(); ++i)
cout << v[i] << " ";
cout << endl;
}
static void writeMatrix(vector<vector<T>>& v)
{
for (int i = 0; i < v.size(); ++i)
{
for (int j = 0; j < v[i].size(); ++j)
cout << v[i][j] << " ";
cout << endl;
}
}
};
class String
{
public:
static vector<string> split(string& s, char delim)
{
vector<string> ret;
int i = 0, j = 0;
while (j < s.size())
{
if (s[j] == delim)
{
ret.push_back(s.substr(i, j - i));
i = j + 1;
}
++j;
}
ret.push_back(s.substr(i, j - i));
return ret;
}
};
template <typename T>
bool comp(T& a, T& b)
{
return a < b;
}
template <typename T>
bool comp(vector<T>& a, vector<T>& b)
{
if (a.size() != b.size())
return a.size() < b.size();
for (int i = 0; i < a.size(); ++i)
{
if (a[i] != b[i])
return a[i] < b[i];
}
return false;
}
template <typename T>
bool comp(pair<T, T>& a, pair<T, T>& b)
{
if (a.first != b.first)
return a.first < b.first;
return a.second < b.second;
}
template <typename T>
bool compr(T& a, T& b)
{
return a > b;
}
template <typename T>
bool compr(vector<T>& a, vector<T>& b)
{
if (a.size() != b.size())
return a.size() > b.size();
for (int i = 0; i < a.size(); ++i)
{
if (a[i] != b[i])
return a[i] > b[i];
}
return false;
}
template <typename T>
bool compr(pair<T, T>& a, pair<T, T>& b)
{
if (a.first != b.first)
return a.first > b.first;
return a.second > b.second;
}
template <typename T>
void swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
template <typename T>
void reverse(vector<T>& v)
{
int i = 0, j = v.size() - 1;
while (i < j)
{
swap(v[i], v[j]);
++i;
--j;
}
}
}
#endif // !BASICS_H