-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_input_output.cpp
122 lines (113 loc) · 2.92 KB
/
fast_input_output.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
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
// second fast input output
static const int __ = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
int init = [] {
ofstream out("user.out");
cout.rdbuf(out.rdbuf());
for (string str; getline(cin, str); cout << '\n') {
if (str.find(',') == string::npos) {
cout << str;
continue;
}
stringstream ss(str);
ss.ignore();
int prev;
ss >> prev;
ss.ignore();
cout << '[' << prev;
for (int curr; ss >> curr; ss.ignore()) {
cout << ',' << gcd(prev, curr) << ',' << curr;
prev = curr;
}
cout << ']';
}
exit(0);
return 0;
}();
// second fast input output method in c++
class Solution {
public:
static inline auto _ = [] {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::ofstream out("user.out", std::ios::out | std::ios::binary);
out.rdbuf()->pubsetbuf(nullptr, 256);
std::string s;
std::noskipws(std::cin);
while (std::getline(std::cin, s)) {
int count = 0;
bool inNum = false;
for (char c : s) {
if (std::isdigit(c) != 0) {
if (!inNum) {
++count;
inNum = true;
}
} else {
inNum = false;
}
}
out << count << '\n';
}
std::skipws(std::cin);
out.flush();
exit(0);
return 0;
}();
int countNodes(TreeNode *root) {
if (root == nullptr)
return 0;
int left = countNodes(root->left);
int right = countNodes(root->right);
return 1 + left + right;
}
};
// third approach .. this is the fastest in leetcode
int a[50001];
int init = [] {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ofstream out("user.out");
for (string s; getline(cin, s);) {
if (s.length() == 2) {
out << "[]\n";
continue;
}
int n = 0;
for (int _i = 1, _n = s.length(); _i < _n; ++_i) {
bool _neg = false;
if (s[_i] == '-')
++_i, _neg = true;
int v = s[_i++] & 15;
while ((s[_i] & 15) < 10)
v = v * 10 + (s[_i++] & 15);
if (_neg)
v = -v;
a[n++] = v;
}
sort(a, a + n);
out << '[' << a[0];
for (int i = 1; i < n; ++i)
out << ',' << a[i];
out << "]\n";
}
out.flush();
exit(0);
return 0;
}();
const static auto initialize = [] {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return nullptr;
}();