ios::sync_with_stdio(0);
cin.tie(0);
// ...
To redirect the standard input and output streams from your program into a file named output.txt
(works for both cout
and printf
):
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// ...
}
NOTE: Some Codeforces problems actually require input.txt
& output.txt
. In those cases the #ifndef
preprocessor conditional directive should be removed.
int TC;
cin >> TC;
while (TC--)
// ...
Useful for problems with indefinite input (e.g. until EOF or a specific value).
while (true)
{
int x;
cin >> x;
if (x == 0)
break; // Sentinel value ends the loop
// ...
}
Snippet: readintarr
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++)
cin >> v[i].first >> v[i].second;
For problems requiring selective input parsing.
E.g. A_Where_Are_My_Flakes.cpp
string _, t;
int k;
cin >> _ >> _ >> t >> _ >> k; // We're only interested in t and k
For problems where input size isn’t provided.
vector<int> v;
int x;
while (cin >> x)
v.push_back(x);
E.g. 11586_Train Tracks.cpp
string s;
getline(cin, s); // e.g. MM FF MF FM
int len = s.size();
for (int i = 0; i < len; i++)
// ...
typedef pair<int, int> ii;
typedef vector<ii> vii;
int dp[n + 1];
memset(dp, -1, sizeof(dp));
dp[0] = 0;
memset(a, 0, sizeof(a));
Set all elements false
bool myBoolArray[ARRAY_SIZE] = { 0 };
Snippet: readmatrix
int n, m;
cin >> n >> m;
vector<string> matrix(n);
for (int i = 0; i < n; i++)
cin >> matrix[i];
E.g. A. Ultra-Fast Mathematician.cpp
, A. Chewbaсca and Number.cpp
int n = s[i] - '0';
// ...
ans += to_string(n);
charMatrix[i][j] = char(n);
DO NOT use this for production code: Redefining int
for the entire program can be very inefficient and potentially dangerous. This hack only makes sense in the context of Competitive Programming problems. Use int64_t
instead.
#define int long long
// Then can use `int32_t` for actual `int` values if needed.
string s;
cin >> s;
for (char c : s)
// ...
Splitting a string into words based on delimiters.
string s = "word1 word2 word3";
stringstream ss(s);
string word;
while (ss >> word)
// ...
string s = "word1,word2,word3";
stringstream ss(s);
string word;
while (getline(ss, word, ',')) // Use comma as delimiter
// ...
int idx = str.find(c);
if (idx != string::npos)
cout << "Found";
void print_vector(vector<int> v)
{
stringstream ss;
for (int i = 0; i < v.size(); i++)
{
if (i != 0)
ss << " ";
ss << v[i];
}
cout << ss.str();
}
sort(a, a + SIZE); // (sort up to the last element in the array)
sort(v.begin(), v.end());
sort(v.begin(), v.end(), greater<int>());
sort(v.begin(), v.end(), [](pair<int, int> &a, pair<int, int> &b)
{
return a.second < b.second; // based on 2nd element
});
GeeksForGeeks
E.g. B. Roma and Changing Signs.cpp
cout << accumulate(a, a + n, 0);
auto it = lower_bound(a.begin(), a.end(), target); // First >= target
auto it = upper_bound(a.begin(), a.end(), target); // First > target
E.g. B. Pashmak and Flowers.cpp
int mn = *min_element(a, a + n);
int mx = *max_element(a, a + n);
E.g. B. Shower Line.cpp
int line[N] = { ... };
do
{
// ...
} while (next_permutation(line, line + N));