C++ tips for algorithm problem-solving
Tools, tricks, and snippets are introduced for implementing algorithms. Some may be inappropriate in terms of good coding practice because these snippets are optimized for efficiency during competitive problem-solving.
Arrays
Arrays are an important tool for storing sequential data. Also, a lot of problems are solved with dynamic programming, where arrays are a core tool.
Initializing an array with a single value
-
For loop
for (int i=0; i<n; ++i) { d[i] = 0; }A standard \(O(n)\) algorithm that fills array
dwith0. -
Using
memset(void* ptr, int value, size_t num)memset(d, 0, sizeof(d)); // #include <cstring>Note that the
valueparameter should be either0or-1. This is becausememsetwas originally designed for filling every byte of a string with the same value. Specifically, sincevalueis interpreted as anunsigned charof size 1 byte, settingvalueto1fills every byte of the array with0x01. Thus accessing a 4 byte integer gives0x01010101 == 16843009. On the contrary, settingvalueto-1fills every byte with0xff, where in this case grouping these bytes into 4 byte integers retains the 2’s complement representation of-1, which is0xffffffff. -
Declaring as a global variable
#include <cstdio> int d[10]; int main() { printf("%d", d[0]); return 0; }Variables delcared as a global variable are intialized to zero automatically.
-
Instead using
std::vectorvector<int> v(n, -1); // #include <vector>Using vectors is a preferable in many cases, especially when the size of the array varies. For instance, it is a good idea to initialize graphs with
vector<vector<int>> graph(n);as an adjacency list. This done, you can easily traverse adjacent nodes with the following range-based for loop:for (int adj_node : graph[curr_node])
Array Indexing
-
Single dimensional array
int a[50]; for (int i=0; i<50; ++i) { scanf("%d", a+i); }The pointer of an array element can be written simpler.
for (int i=0; i<n; ++i) { printf("%c%d%c", " ["[i==0], a[i], ",]"[i==n-1]); }This snippet prints array
apython-style:[0, 1, 2, 3, 4] -
Two dimensional array
printf("%d\n", *max_element(d[0], d[0]+n)); // #include <algorithm>The element of a two dimensional array is a pointer to a single dimensional array. Thus the same trick can be applied.
Standard Input and Output
Avoid using cin and cout
For algorithm problems, small time can make a difference. Especially if you’re reading somthing like \(n^2\) integers from standard input, using cin can give you a TLE(Time Limit Exceeded). Use scanf for inputs and printf for outputs.
Use text files for standard input
Typing in the same sample inputs every time you run your code is apparently inefficient. When compiling and running your code, you can provide “input.txt” as a standard input using the above shell command. You can read data with your ordinary standard i/o functions.
g++ -O2 -std=c++14 main.cpp -o main && ./main < input.txt
Leave a comment