In algorithmic competitions, fast reading is a common technique used to increase the speed of inputting data. There are several common methods of fast reading:
1. Fast Reading in C++
Commonly used in C++scanf
cap (a poem)getchar
Perform a quick read.
#include <cstdio>
#include <cstring>
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
2. Fast Reading in Python
Pythoninput()
Relatively slow, you can use to increase speed.
import sys
input =
data = input().split()
3. Fast Reading in Java
In Java, you can use theBufferedReader
cap (a poem)StringTokenizer
。
import .*;
import ;
public class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader());
}
String next() {
while (st == null || !()) {
try {
st = new StringTokenizer(());
} catch (IOException e) {
();
}
}
return ();
}
int nextInt() {
return (next());
}
}
4. summarize
The principle of fast reading is mainly based on reducing the number of input operations and using more efficient input methods, thus improving the overall performance of the program. Here are a few key points:
1. Reduced system calls
- On standard input, each call to
scanf
maybeinput()
All of them make a system call, which can be time-consuming. Fast reading reduces this call by reading a large amount of data at once and processing it in memory.
2. Using the buffer
- Fast reads typically use a buffer to temporarily store input data, utilizing memory for much faster reads than character-by-character reads. For example, by
getchar
maybeBufferedReader
and other methods to read an entire row or multiple rows of data at once.
3. string processing (computing)
- After reading the data, it is usually stored as a string and then parsed by separators (e.g., spaces, newlines). This allows you to quickly extract the required data without having to call the input function every time.
4. character processing
- Fast reading usually uses character processing, such as reading character by character until a number or a specific format is found, and can efficiently handle basic data types such as integers or floating point numbers.
5. Avoiding type conversions
- In some implementations, it is possible to convert input characters directly to numbers, reducing the need to use functions such as
atoi
of time overhead to further increase speed.
6. Overall efficiency
- With the above method, Fast Read can significantly reduce the total input time and improve the efficiency of the program when dealing with large amounts of data, especially in tournaments, where the efficiency of the input and output has a direct impact on the overall running time.
Using quick reads can help players save valuable time in algorithmic competitions and improve problem solving efficiency.
Fast reading can significantly improve input efficiency, especially when working with large amounts of data. Choosing the right fast-reading method can help save time during a competition. When using it, pay attention to the format of the data and boundary conditions.