-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_snippets.json
638 lines (637 loc) · 14.8 KB
/
cpp_snippets.json
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
{
"Initialize C++ Main()": {
"prefix": "init",
"body": [
"#include <iostream>",
"using namespace std;",
"",
"using ll = long long;",
"int main()",
"{",
" ${0:// let the game begin!}",
" return 0;",
"}"
],
"description": "Initialize C++ Main()"
},
"CP-Initialize C++ Main()": {
"prefix": "init-cp",
"body": [
"#include <bits/stdc++.h>",
"#include<ext/pb_ds/assoc_container.hpp>",
"#include<ext/pb_ds/tree_policy.hpp>",
"",
"using namespace std;",
"using namespace __gnu_pbds;",
"",
"using ll = long long;",
"using ld = long double;",
"",
"#define endl \"\\n\"",
"#define all(x) (x).begin(), (x).end()",
"",
"const ld pi = acos(-1);",
"const ll MOD = 1000000007;",
"const ll N = 1e5+7;",
"",
"template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;",
"",
"signed main()",
"{",
" ios::sync_with_stdio(false);",
" cout.tie(nullptr);",
"",
" ${0:// let the game begin!}",
"",
" return 0;",
"}"
],
"description": "CP-Initialize C++ Main()"
},
"While-TestCase": {
"prefix": "tw",
"body": [
"ll tc; cin >> tc;",
"while(tc--)",
"{",
" ${0:let the game begin}",
"}"
],
"description": "While-TestCase"
},
"For-TestCase": {
"prefix": "tf",
"body": [
"ll n; cin >> n;",
"for(ll tc = 1; tc <=n; tc++)",
"{",
" // printf(\"Case %d:\", tc);",
" ${0:let the game begin}",
"}"
],
"description": "For-TestCase"
},
"While-TestCase with Special Value": {
"prefix": "twsv",
"body": [
"ll a,b;",
"while (scanf(\"%lld %lld\", &a, &b), (a||b))",
"{",
" ${0:let the game begin};",
"}"
],
"description": "While-TestCase with Special Value"
},
"While-TestCase with EOF": {
"prefix": "tweof",
"body": [
"ll a,b;",
"while (scanf(\"%lld %lld\", &a, &b) != EOF)",
"{",
" ${0:let the game begin};",
"}"
],
"description": "While-TestCase with EOF"
},
"Print Vector": {
"prefix": "prnt_vec",
"body": [
"for(ll unsigned i = 0;i < ${1:vec.size()}; i++)",
"{",
" cout << vec[i] << \" \";",
"}",
"cout << endl;"
],
"description": "Print Vector"
},
"Initializes vector with Input": {
"prefix": "ivec",
"body": [
"ll ${1:n}; cin >> ${1:n};",
"vector<ll> ${2:vec}(${1:n});",
"for(ll i =0; i < ${1:n}; i++)",
"{",
" ll temp_vec_v; cin >> temp_vec_v;",
" ${2:vec}.push_back(temp_vec_v);",
"}"
],
"description": "Initializes vector with Input"
},
"Initialize Vector with Size": {
"prefix": "vec_n",
"body": [
"vector<${1:ll}> ${2:vec}(${3:n});"
],
"description": "Initialize Vector with Size"
},
"Initialize Vector with Values": {
"prefix": "vec",
"body": [
"vector<${1:ll}> ${2:vec};"
],
"description": "Initialize Vector with Values"
},
"Convert Int To Binary": {
"prefix": "conv_inttobits",
"body": [
"bitset <${1:8}> ${2:b_int}($3);"
],
"description": "Convert Int To Binary"
},
"Print COUT": {
"prefix": "print",
"body": [
"cout << $0 << endl;"
],
"description": "Print COUT"
},
"Get CIN": {
"prefix": "scan",
"body": [
"cin >> $0 ;"
],
"description": "Get CIN"
},
"Initialize Function For Printing 1D Array": {
"prefix": "prnt_Farr",
"body": [
"void printArray(ll arr[], ll n){",
" for(ll i=0; i < n; ++i)",
" {",
" cout << arr[i] << \" \";$1",
" }",
"",
" cout << endl;$2",
"}"
],
"description": "Initialize Function For Printing 1D Array"
},
"Print Array": {
"prefix": "prnt_arr",
"body": [
"// initialize printArr function first using prnt_Farr",
"printArray(${1:arr[]}, ${2:n});"
],
"description": "Print Array"
},
"Initialize Array[size] With Input": {
"prefix": "iarr",
"body": [
"ll ${1:arr_length}; cin >> ${1:arr_length};",
"ll ${2:arr}[${1:arr_length}];",
"for(ll i = ${3:0}; i < ${1:arr_length}; i++) cin >> arr[i];"
],
"description": "Initialize Array[size] With Input"
},
"(vec,ll) Returns Index using Binary Search Function": {
"prefix": "Fbs",
"body": [
"ll binary_search(vector<ll> ar, ll x)",
"{",
" ll n = ar.size();",
" ll l =0;",
" ll r= n-1;",
" while(l<=r)",
" {",
" ll mid=l + (r-l)/2;",
" if(ar[mid] == x)",
" {",
" return mid;",
" }else if(ar[mid]>x)",
" {",
" r = mid - 1;",
" }else",
" {",
" l = mid + 1;",
" }",
" }",
" return -1;",
"}"
],
"description": "(vec) Returns Index using Binary Search Function"
},
"(vec,ll) Returns Index using Binary Search": {
"prefix": "bs",
"body": [
"// requires Fbs",
"ll bs_ind = binary_search(${2:vec},${3:number});"
],
"description": "Returns Index using Binary Search"
},
"(ll) Returns 1 or 0 if Prime or Not": {
"prefix": "Fprime",
"body": [
"ll isPrimeNumber(ll x)",
"{",
" if(x<2) return 0;",
" if(x == 2 || x == 3) return 1;",
" if(x % 2 == 0) return 0;",
"",
" for(int i = 3; i*i <= x; i+=2){",
" if(x % i == 0) return 0;",
" }",
" ",
" return 1;",
"}"
],
"description": "(ll) Returns 1 or 0 if Prime or Not"
},
"Initialize Prime Checker Variable": {
"prefix": "prime",
"body": [
"// requires Fprime",
"ll ispn = isPrimeNumber(${1:number});"
],
"description": "Initialize Prime Checker Variable"
},
"Generate Prime Numbers Till N": {
"prefix": "gen_Fprime",
"body": [
"vector<ll> sieve_of_eratosthenes(ll n)",
"{",
" vector<ll> primes;",
" ll arr[n+1] = {0};",
" arr[1] = 1;",
" for(ll i = 2; i*i <= n; i++)",
" {",
" if(arr[i] == 0)",
" {",
" for(ll j = i*i; j <=n; j+= i)",
" {",
" arr[j] = 1;",
" }",
" }",
" }",
"",
" for(ll i = 2; i <= n; i++)",
" {",
" if(arr[i] == 0)",
" {",
" primes.push_back(i);",
" }",
" }",
"",
" return primes;",
"}",
""
],
"description": "Generate Prime Numbers Till N"
},
"(vector) Initialize Prime Numbers Generator": {
"prefix": "gen_prime",
"body": [
"// requires gen_Fprime",
"vector<ll> vec = sieve_of_eratosthenes(${1:number});"
],
"description": "(vector) Initialize Prime Numbers Generator"
},
"DFS Function": {
"prefix": "Fdfs",
"body": [
"vector<ll> nodeswithedges[N];",
"vector<ll> dfs_nodes;",
"ll visited[N] ={0};",
"void dfs(int rootNode)",
"{",
" //preorder",
" visited[rootNode] = 1;",
" dfs_nodes.push_back(rootNode);",
" //inorder",
" for(int i = 0; i < nodeswithedges[rootNode].size(); i++)",
" {",
" if(visited[nodeswithedges[rootNode][i]] == 0)",
" {",
" dfs(nodeswithedges[rootNode][i]);",
" }",
" }",
" //postorder",
"}"
],
"description": "DFS Function"
},
"Initialize DFS": {
"prefix": "dfs",
"body": [
"// requires Fdfs",
"int totalNodes, rootNode; cin >> totalNodes >> rootNode;",
"int l = totalNodes;",
"while(l--)",
"{",
" int first_node,last_node; cin >> first_node >> last_node;",
" nodeswithedges[first_node].push_back(last_node);",
" nodeswithedges[last_node].push_back(first_node);",
"}",
"dfs(${1:rootNode});",
"// DFS has been generated in dfs_nodes vector"
],
"description": "Initialize DFS"
},
"BFS Function (bfs_nodes)": {
"prefix": "Fbfs",
"body": [
"int visited[N];",
"int level[N];",
"vector<int>adj_list[N];",
"vector<int>bfs_nodes[N];",
"void bfs(int src) {",
" queue<int>q;",
" visited[src] = 1;",
" level[src] = 0; // new line added",
" q.push(src);",
"",
" while(!q.empty()) {",
" int head = q.front();",
" bfs_nodes->push_back(head);",
" q.pop();",
"",
" for(int adj_node: adj_list[head]) {",
" if(visited[adj_node] == 0) {",
" visited[adj_node] = 1;",
"",
" level[adj_node] = level[head] + 1; // new line added",
"",
" q.push(adj_node);",
" }",
" }",
" }",
"}"
],
"description": "BFS Function (bfs_nodes)"
},
"(root,edge) BFS Initialize (bfs_nodes)": {
"prefix": "bfs",
"body": [
"int root, edges;",
" cin >> root >> edges;",
"",
" for(int i = 0 ; i < edges ; i++) {",
" int u, v;",
" cin >> u >> v;",
" adj_list[u].push_back(v);",
" adj_list[v].push_back(u);",
" }",
"",
" bfs(root);"
],
"description": "(root,edge) BFS Initialize (bfs_nodes)"
},
"GCD Function": {
"prefix": "Fgcd",
"body": [
"ll gcd(ll a, ll b)",
"{",
" if(b==0) return a;",
" return gcd(b, a%b);",
"}"
],
"description": "GCD Function"
},
"(ll) Returns GCD": {
"prefix": "gcd",
"body": [
"// requires Fgcd",
"gcd(${1:a},${2:b})"
],
"description": "(ll) returns GCD"
},
"(ll) LCM Function": {
"prefix": "Flcm",
"body": [
"ll lcm(int a, int b)",
"{",
" return (a*b)/__gcd(a,b);",
"}"
],
"description": "(ll) LCM Function"
},
"(ll) Returns the LCM Number": {
"prefix": "lcm",
"body": [
"// requires Flcm",
"lcm(${1:a},${2:b})"
],
"description": "(ll) Returns the LCM Number"
},
"(vector) Generates Prime Factors": {
"prefix": "gen_Fprimefactors",
"body": [
"vector<ll> prime_factors(ll n)",
"{",
" vector<ll> tmp;",
" for(int i=2; i*i <= n; i++)",
" {",
" while(n%i == 0)",
" {",
" n/=i;",
" tmp.emplace_back(i);",
" }",
" }",
"",
" if(n>1) tmp.emplace_back(n);",
"",
" return tmp;",
"}"
],
"description": "(vector) Generates Prime Factors"
},
"(vector) Initialize Prime Factors": {
"prefix": "gen_primefactors",
"body": [
"// requires gen_Fprimefactors",
"vector<ll> vec = prime_factors(${1:number});"
],
"description": "(vector) Initialize Prime Factors"
},
"(base^upper) Power Function": {
"prefix": "Fpower",
"body": [
"ll power(ll base, ll upper) // , ll mod",
"{",
" ll result = 1;",
" while(upper)",
" {",
" if(upper % 2 == 1)",
" {",
" result = (result * base); // %mod",
" upper--;",
" }else",
" {",
" base = (base * base); // %mod",
" upper/=2;",
" }",
" }",
" return result;",
"}"
],
"description": "(base^upper) Power Function"
},
"Do Power (base^powervar)": {
"prefix": "power",
"body": [
"// requires Fpower",
"power(${1:base},${2:powervar})"
],
"description": "Do Power (base^powervar)"
},
"(string) Convert Number to String Function": {
"prefix": "conv_Fnumtostr",
"body": [
"string numtostr(ll num)",
"{",
" return to_string(num);",
"}"
],
"description": "(string) Convert Number to String"
},
"(ll) Convert String to Number Function": {
"prefix": "conv_Fstrtonum",
"body": [
"ll strtonum(string s)",
"{",
" return stoi(s);",
"}"
],
"description": "(ll) Convert String to Number Function"
},
"(ll) Convert String to Number": {
"prefix": "conv_strtonum",
"body": [
"// requires conv_Fstrtonum",
"ll ${1:x} = strtonum(${2:str});"
],
"description": "(ll) Convert String to Number"
},
"(string) Convert Number to String": {
"prefix": "conv_numtostr",
"body": [
"// requires conv_Fnumtostr",
"string ${1:x} = numtostr(${2:number});"
],
"description": "(string) Convert Number to String Function"
},
"Initialize 2D Array": {
"prefix": "arr_2d",
"body": [
"ll ${1:arr_length}; cin >> ${1:arr_length};",
"ll ${2:arr}[${1:arr_length}][${1:arr_length}];",
"for(ll i = 0; i < ${1:arr_length}; i++)",
"{",
" for (ll j = 0; j < ${1:arr_length}; j++)",
" {",
" cin >> ${2:arr}[i][j];",
" } ",
"}"
],
"description": "Initialize 2D Array"
},
"Print 2D Array": {
"prefix": "prnt_arr_2d",
"body": [
"for(ll i = 0; i < ${1:arr_length}; i++)",
"{",
" for (ll j = 0; j < ${1:arr_length}; j++)",
" {",
" cout << ${2:arr}[i][j] << \" \";",
" } ",
" cout << endl;",
"}"
],
"description": "Print 2D Array"
},
"Longest Common Subsequence Length Function": {
"prefix": "Flcs",
"body": [
"ll lcs(string s1, string s2)",
"{",
" ll str1_size = s1.size();",
" ll str2_size = s2.size();",
" cout << str1_size << \" \" << str2_size << endl;",
"",
" ll arr[str1_size+1][str2_size+1];",
" memset(arr, 0, sizeof(arr));",
"",
" for(ll i = 1; i <= str1_size; i++)",
" {",
" for(ll j = 1; j <= str2_size; j++)",
" {",
" if(s1[i-1] == s2[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[str1_size-1][str2_size-1];",
"}"
],
"description": "Longest Common Subsequence Length Function"
},
"Longest Common Subsequence Length Between two strings": {
"prefix": "lcs",
"body": [
"// requires Flcs",
"lcs(${1:s1}, ${2:s2})"
],
"description": "Longest Common Subsequence Length Between two strings"
},
"Initialize Ordered List": {
"prefix": "olist",
"body": [
"ordered_set<${1:ll}> ${2:os};"
],
"description": "Initialize Ordered List"
},
"Binary Exponent Function": {
"prefix": "Fbe",
"body": [
"ll binary_exponent(ll x, ll y)",
"{",
" ll ans = 1;",
" while(y > 0)",
" {",
" if(y & 1) ans = (ans * x);",
" x = (x * x);",
" y >>= 1;",
" }",
" return ans;",
"}"
],
"description": "Binary Exponent Function"
},
"Initialize Binary Exponent Function": {
"prefix": "be",
"body": [
"// requires Fbe",
"ll ${1:be_val} = binary_exponent(${2:x}, ${3:y});"
],
"description": "Initialize Binary Exponent Function"
},
"Modular Exponentiation Function": {
"prefix": "Fme",
"body": [
"ll modular_exp(ll x, ll y, ll mod)",
"{",
" ll ans = 1;",
" while(y > 0)",
" {",
" if(y & 1) ans = (ans * x) % mod;",
" x = (x * x) % mod;",
" y >>= 1;",
" }",
" return ans;",
"}"
],
"description": "Modular Exponentiation Function"
},
"Initilaize Modular Exponentiation Function": {
"prefix": "me",
"body": [
"// requires Fme",
"ll ${1:me_val} = modular_exp(${2:x}, ${3:y}, ${4:mod});"
],
"description": "Initilaize Modular Exponentiation Function"
}
}