LeetCode模板

在LeetCode上提交的C代码并不需要include标准库头文件,判题系统会自动包含,并且在二叉树的题目会额外包含TreeNode结构。我希望有一个简洁的main.cpp可以直接提交到LeetCode,里面包含一些最常用的函数,调试输出的代码放在bits/stdc.h中(故意和标准库头文件同名),通过宏定义只在引入本地自定义的bits/stdc++.h时开启cout输出,这样可以直接把main.cpp的全部内容直接提交到网站条件,会自动屏蔽掉所有调试输出代码而不会报错。

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
#include "bits/stdc++.h"
using namespace std;

#define all(a) (a).begin(), (a).end()
template<typename T, typename F=less<T>>
using pque=priority_queue<T, vector<T>, F>;
template<typename T, typename F=less<T>>
pque<T, F> make_pque(F cmp) { return pque<T, F>(cmp); }
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef vector<vs> vvs;

template <typename T1, typename T2>
auto max(T1 a, T2 b) -> decltype(a + b) { return a>b?a:b; }
template <typename T1, typename T2>
auto min(T1 a, T2 b) -> decltype(a + b) { return a<b?a:b; }
template <typename T1, typename T2, typename T3>
auto max(T1 a, T2 b, T3 c) -> decltype(a + b + c) { return max(max(a, b), c); }
template <typename T1, typename T2, typename T3>
auto min(T1 a, T2 b, T3 c) -> decltype(a + b + c) { return min(min(a, b), c); }
template <typename T>
T max(const vector<T>& a) {T r = a[0]; for(auto e : a) r = max(r, e); return r;}
template <typename T>
T min(const vector<T>& a) {T r = a[0]; for(auto e : a) r = min(r, e); return r;}
template<typename T>
T sum(const vector<T>& a) { T r = 0; for(auto& e : a) r+=e; return r;}
template<typename T>
T gcd(T a, T b) { while(b) { T r = a%b; a = b; b = r;} return a;}
template<typename T>
T lcm(T a, T b) { return a/gcd(a,b)*b; }
template<typename F>
ll lb(ll b, ll e, F f) {if(b>=e) return e; while(b<e-1) {auto m=b+(e-1-b)/2; if(!f(m)) b=m+1; else e=m+1;} return f(b)?b:e;}
template<typename F>
ll ub(ll b, ll e, F f) {return lb(b, e, [&](ll i){return !f(i);});}

template<typename T>
struct cast_helper {T operator() (stringstream& ss) {T r=T{}; ss >> r; return r;}};
template<>
struct cast_helper<string> {string operator() (stringstream& ss) { return ss.str();}};
template<typename R, typename T>
R sstream_cast(const T& o) {stringstream ss; ss << o; return cast_helper<R>()(ss);}
string format(const char* f, ...){va_list a; va_start(a, f); char b[4096]; vsnprintf(b, 4096, f, a); va_end(a); return b;}
template<typename T>
unordered_map<T, int> counter(const vector<T>& a) {unordered_map<T, int> r;for(auto e : a) ++r[e];return r;}
unordered_map<char, int> counter(const string& a) {unordered_map<char, int> r;for(auto e : a) ++r[e];return r;}
template<typename I>
vector<I> range(I b, I e) {vector<I> r(e-b);iota(all(r), b);return r;}

vvi make_vvi(int n, int m, int v=0) { return vvi(n, vi(m, v));}
vvb make_vvb(int n, int m, bool v=false) { return vvb(n, vb(m, v));}
vvs make_vvs(int n, int m, const string& v="") { return vvs(n, vs(m, v));}
typedef tuple<int, int> tii;
typedef tuple<ll, ll> tll;
typedef tuple<int, int, int> tiii;
#define _0(o) get<0>(o)
#define _1(o) get<1>(o)
#define _2(o) get<2>(o)
namespace std {
template<>struct hash<tii>{size_t operator()(const tii& a)const {return _0(a)^_1(a);}};
template<>struct hash<tiii>{size_t operator()(const tiii& a)const {return _0(a)^_1(a)^_2(a);}};
};
tii dir4[] = {tii(-1, 0), tii(0, 1), tii(1, 0), tii(0,-1)};
tii dir8[] = {tii(-1, 0), tii(0, 1), tii(1, 0), tii(0,-1), tii(-1,-1), tii(-1,1), tii(1,1), tii(1,-1)};
tii& operator += (tii& a, const tii& b) { _0(a)+=_0(b); _1(a)+=_1(b); return a; }
tii operator + (const tii& a, const tii& b) { return tii(_0(a)+_0(b), _1(a)+_1(b)); }
tii& operator -= (tii& a, const tii& b) { _0(a)-=_0(b); _1(a)-=_1(b); return a; }
tii operator - (const tii& a, const tii& b) { return tii(_0(a)-_0(b), _1(a)-_1(b)); }
tii operator - (const tii& a) { return tii(-_0(a), -_1(a)); }
tii& operator *= (tii& a, int k) { _0(a)*=k; _1(a)*=k; return a; }
tii operator * (int k, const tii& a) { return tii(k*_0(a), k*_1(a)); }
tii operator * (const tii& a, int k) { return tii(_0(a)*k, _1(a)*k); }
tii& operator /= (tii& a, int k) { _0(a)/=k; _1(a)/=k; return a; }
tii operator / (const tii& a, int k) { return tii(_0(a)/k, _1(a)/k); }
bool in_range(tii p, tii e) {return 0<=_0(p)&&_0(p)<_0(e)&&0<=_1(p)&&_1(p)<_1(e);}
bool in_range(tii p, tii b, tii e) {return _0(b)<=_0(p)&&_0(p)<_0(e)&&_1(b)<=_1(p)&&_1(p)<_1(e);}

constexpr int INF = 1e9+7;
constexpr int MOD = 1e9+7;

#ifndef cout
struct _ {
template <typename T>
_& operator << (const T&){ return *this; }
};
#define cout _()
#define endl '\n'
#endif


int _main_()
{

return 0;
}

#undef cout
#undef endl

一定要在结尾undef掉,不然会导致LeetCode误判

bits/stdc++.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// #include <bits/stdc++.h>
#include <iostream>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
#include <tuple>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <iomanip>
#include <thread>
#include <chrono>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdarg>
using namespace std;

template<typename T>
T _s(const T& t) {return t;}
string _s(const string& t) {return '"'+ t + '"';}

template<typename T1, typename T2>
ostream& operator << (ostream& out, const pair<T1, T2>& o) {
return out << "(" << o.first << ", " << o.second << ")";
}

template<typename T>
ostream& operator << (ostream& out, const vector<T>& v) {
out << "[";
for(int i=0;i<v.size();i++) {
out << _s(v[i]) << (i != v.size() -1 ? ", " : "");
}
return out << "]";
}

template<typename T>
ostream& operator << (ostream& out, const vector<vector<T>>& v) {
out << "[";
for(int i=0;i<v.size();i++) {
out << (i!=0?" ":"") << "[";
for(int j=0;j<v[i].size();j++) {
out << setw(5) << _s(v[i][j]) << (j != v[i].size() -1 ? ", " : "");
}
out << "]" << (i != v.size() -1 ? ",\n" : "");
}
return out << "]";
}

template<typename TK, typename TV>
ostream& operator << (ostream& out, const map<TK,TV>& m) {
out << "{";
auto itr=m.begin();
if(itr != m.end()) {
out << *itr;
for(itr++;itr!=m.end();itr++) {
out << ", " << *itr;
}
}
return out << "}";
}

template<typename TK, typename TV>
ostream& operator << (ostream& out, const multimap<TK,TV>& m) {
out << "{";
auto itr=m.begin();
if(itr != m.end()) {
out << *itr;
for(itr++;itr!=m.end();itr++) {
out << ", " << *itr;
}
}
return out << "}";
}

template<typename TK, typename TV>
ostream& operator << (ostream& out, const unordered_map<TK,TV>& m) {
out << "{";
auto itr=m.begin();
if(itr != m.end()) {
out << *itr;
for(itr++;itr!=m.end();itr++) {
out << ", " << *itr;
}
}
return out << "}";
}

template<typename T>
ostream& operator << (ostream& out, const set<T>& m) {
out << "{";
auto itr=m.begin();
if(itr != m.end()) {
out << *itr;
for(itr++;itr!=m.end();itr++) {
out << ", " << *itr;
}
}
return out << "}";
}

template<typename T>
ostream& operator << (ostream& out, const multiset<T>& m) {
out << "{";
auto itr=m.begin();
if(itr != m.end()) {
out << *itr;
for(itr++;itr!=m.end();itr++) {
out << ", " << *itr;
}
}
return out << "}";
}

template<typename T>
ostream& operator << (ostream& out, const unordered_set<T>& m) {
out << "{";
auto itr=m.begin();
if(itr != m.end()) {
out << *itr;
for(itr++;itr!=m.end();itr++) {
out << ", " << *itr;
}
}
return out << "}";
}

template <size_t N>
struct PrintHelper;

template <>
struct PrintHelper<1>
{
template<typename... Args>
static void recursive_print(ostream& out, const tuple<Args...> t)
{
out << "(" << std::get<0>(t) << ", ";
}
};

template <size_t N>
struct PrintHelper
{
template<typename... Args>
static void recursive_print(ostream& out, const tuple<Args...> t)
{
PrintHelper<N - 1>::recursive_print(out, t);
out << std::get<N - 1>(t) << ", ";
}

template<typename... Args>
static void print(ostream& out, const tuple<Args...> t)
{
PrintHelper<N - 1>::recursive_print(out, t);
out << std::get<N - 1>(t) << ")";
}
};

template <typename... Args>
ostream& operator << (ostream& out, const tuple<Args...> t)
{
PrintHelper<tuple_size<decltype(t)>::value >::print(out, t);
return out;
}

template<typename T>
struct _cast_helper
{
T operator() (stringstream& ss) {
T result;
ss >> result;
return result;
}
};

template<>
struct _cast_helper<string>
{
string operator() (stringstream& ss) {
return ss.str();
}
};

template<typename R, typename T>
R _sstream_cast(const T& o) {
stringstream ss;
ss << o;
return _cast_helper<R>()(ss);
}

template<typename T>
vector<T> split(const string& s, const string& delim, const string& stripchars="", bool drop_empty=false) {
vector<T> result;
int b = 0;
int e = 0;
int i = b;
int state = 0;
do {
bool isspace = (stripchars.find(s[i]) != -1);
bool isdelim = (s[i]=='\0' || delim.find(s[i]) != -1);
if(isdelim) {
if(e != b || !drop_empty) {
result.emplace_back(_sstream_cast<T>(string(&s[b], &s[e])));
}
state = 0;
e = b = i + 1;
} else if(isspace) {
if(state == 0) {
e = b = i + 1;
}
} else {
state = 1;
e = i + 1;
}
if(s[i]=='\0') break;
i++;
} while(true);
return result;
}

typedef vector<int> vi;
typedef vector<vi> vvi;
vi make_vi(const string& s) {return split<int>(s, ",", "[] ", true);}
vvi make_vvi(const string& s) { vvi r; for(auto e : split<string>(s, "[]", ", ", true)) r.emplace_back(make_vi(e)); return r;}

/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};

/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


#define cout cout

int _main_();
int main() {
std::thread th1([](){this_thread::sleep_for(chrono::seconds(3));cerr << "!!!Timeout!!!" << endl;exit(1);});
th1.detach();
return _main_();
}