OJ 中使用 fread 加速读入

关于 fread 加速读入的说明请参考 我之前的博文

在使用这段代码的时候我发现在某些题目上容易出错导致 TLE,比如 POJ-1330。

有一些 OJ 的用例可能在两个数字之间隔了多个分隔符,导致输入出错。所以我重新写了一个模板, 贴在这里。现在 sread 函数可以正确处理两个整型之间相隔了多个分隔符的情况,但是仍然不能 处理小数。

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
const int MAXBUF = 1000000 << 2;
char buf[MAXBUF];
char* startpoint = buf;
int BUFLEN;

template<typename T>
inline void sread(T &res)
{
res = 0;

while (startpoint < buf + BUFLEN && !isdigit(*startpoint))
++startpoint;

for (;*startpoint&&startpoint < buf + BUFLEN;++startpoint)
{
if (!isdigit(*startpoint))
{
++startpoint;
return;
}
else
res = res * 10 + *startpoint - '0';
}
}

inline void read_init()
{
BUFLEN = fread(buf, sizeof(char), MAXBUF, stdin);
if (BUFLEN >= MAXBUF)
throw int();
}

在 main 函数的开头调用 read_init() 初始化,之后使用 sread 读取数据即可。

本作品采用 署名-相同方式共享 4.0 国际 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 “不科学的科学君” (Liu233w) 与博客链接: https://liu233w.github.io ,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

加载评论框需要翻墙