[ACM-ICPC] About I/O
-
Upload
chih-hsuan-kuo -
Category
Education
-
view
457 -
download
7
Transcript of [ACM-ICPC] About I/O
![Page 1: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/1.jpg)
About I/O郭至軒(KuoE0)
![Page 2: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/2.jpg)
Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
http://creativecommons.org/licenses/by-sa/3.0/
Latest update: Feb 27, 2013
![Page 3: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/3.jpg)
Standard Input & Output
- 標準輸入- 標準輸出
![Page 4: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/4.jpg)
Standard Input & Output
- 標準輸入- 標準輸出
![Page 5: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/5.jpg)
Standard Input & Output
- 標準輸入- 標準輸出
![Page 6: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/6.jpg)
標準輸入→由鍵盤輸入標準輸出→由螢幕輸出
![Page 7: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/7.jpg)
input file output file
![Page 8: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/8.jpg)
Multiple Test Case
1 23 45 67 8
371115
input file output file
![Page 9: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/9.jpg)
Continuous Processing
read one test caseno datastart
end
output result calculate
![Page 10: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/10.jpg)
read: 1 2start calculate write: 3
read: 3 4calculatewrite: 7read: 5 6
calculate write: 11 read: 7 8 calculate
write: 15end
![Page 11: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/11.jpg)
End Of File若題目未指定測資終止條件,則為判斷 EOF 作為終止
條件!
scanf fgets cin
while (scanf() != EOF){
...}
while (fgets() != 0){
...}
while (cin >> x){
...}
![Page 12: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/12.jpg)
File Input & Outputfopen fstream (C++ only)
FILE *in = fopen(“inputfile”);FILE *out = fopen(“outputfile”);
fscanf(in, ...);fprintf(out, ...);
fclose(in);fclose(out);
ifstream in = ifstream(“inputfile”);
ofstream out = ofstream(“outputfile”);
in >> x;out << x;
in.close();out.close();
![Page 13: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/13.jpg)
If you write the code like previous page in contest, you
will lose the contest.
![Page 14: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/14.jpg)
freopen
redirect the file I/O to standard I/O
freopen(“inputfile”, “r”, stdin);freopen(“output”, “w”, stdout);
scanf(...);printf(...);
![Page 15: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/15.jpg)
I/O Performance
![Page 16: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/16.jpg)
scanf & cin
10^4
10^5
10^6
10^7
0 sec 1 sec 2 sec 3 sec 4 sec
3.45
0.41
0.04
0
1.27
0.14
0.01
0
scanf cin
![Page 17: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/17.jpg)
printf & cout
10^4
10^5
10^6
10^7
0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec
4.02
0.42
0.04
0.01
1.08
0.11
0.01
0
printf cout
![Page 18: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/18.jpg)
Improve Stream I/OStream I/O need to keep themselves in sync with the underlying C library.
std::ios::sync_with_stdio(false);if you won’t use C I/O.
Add this line in code,
![Page 19: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/19.jpg)
cin without sync
10^4
10^5
10^6
10^7
0 sec 1 sec 2 sec 3 sec 4 sec
1.70
0.19
0.02
0
3.45
0.41
0.04
0
1.27
0.14
0.01
0
scanf cincin without sync
![Page 20: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/20.jpg)
cout without sync
10^4
10^5
10^6
10^7
0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec
3.49
0.36
0.04
0.01
4.02
0.42
0.04
0.01
1.08
0.11
0.01
0
printf coutcout without sync
![Page 21: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/21.jpg)
Stream I/O is Still Slow
Recommend to use C I/O.
![Page 22: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/22.jpg)
Buffered Technique
fgets/puts are faster than scanf/printf.Base on fgets/puts function.
![Page 23: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/23.jpg)
Buffered ReadRead mass data, and parse by self.
char buf[ 1000 ];int a, b;fgets( buf, sizeof( buf ), stdin ); sscanf( buf, “%d %d”, &a, &b );
![Page 24: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/24.jpg)
Buffered Write
char buf[ 1000 ];int ret = a + b;sprintf( buf, “%d”, ret );puts( buf );
Store mass data into temporal buffer, and write them once.
![Page 25: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/25.jpg)
Buffering Technique
10^4
10^5
10^6
10^7
0 sec 1 sec 2 sec 3 sec 4 sec
0.53
0.05
0.01
0
1.70
0.19
0.02
0
3.45
0.41
0.04
0
1.27
0.14
0.01
0
scanf cincin without sync fgets
![Page 26: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/26.jpg)
Buffering Technique
10^4
10^5
10^6
10^7
0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec
1.71
0.16
0.02
0
3.49
0.36
0.04
0.01
4.02
0.42
0.04
0.01
1.08
0.11
0.01
0
printf coutcout without sync puts
![Page 27: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/27.jpg)
Advanced Parsing Skill
char* strtok( char *str, const char *delimiters );
strtok Split string into tokens.
str: 欲切割之字串delimiters: 分隔字符字串return value: 指向當前切割字串之指標,若切割完畢則回傳 NULL
![Page 28: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/28.jpg)
strtok
A “corpus” is a collection of texts of written (or spoken) language presented in electronic form.
original string:
take out all words:A corpus is acollection of texts ofwritten or spoken languagepresented in electronic form
![Page 29: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/29.jpg)
char str = “A \“corpus\” is a collection of texts of written (or spoken) language presented in electronic form.”
for ( char *token = strtok( str, “ \”().” ); token != NULL; token = strtok( NULL, “ \”().” ) ) {
puts( token );}
![Page 30: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/30.jpg)
A \“ c o r p u s \” i s a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
delimiters: “ \”().”global pointerstart position
![Page 31: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/31.jpg)
A \“ c o r p u s \” i s a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
\0
delimiters: “ \”().”global pointerstart position
![Page 32: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/32.jpg)
Return address of start position.
“A”
![Page 33: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/33.jpg)
A \0 \“ c o r p u s \” i s a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
delimiters: “ \”().”global pointerstart position
![Page 34: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/34.jpg)
A \0 \“ c o r p u s \” i s a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
\0 \0
delimiters: “ \”().”global pointerstart position
![Page 35: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/35.jpg)
Return address of start position.
“corpus”
![Page 36: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/36.jpg)
A \0 \0 c o r p u s \0 i s a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
delimiters: “ \”().”global pointerstart position
![Page 37: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/37.jpg)
A \0 \0 c o r p u s \0 i s a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
\0 \0
delimiters: “ \”().”global pointerstart position
![Page 38: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/38.jpg)
Return address of start position.
“is”
![Page 39: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/39.jpg)
A \0 \0 c o r p u s \0 \0 i s \0 a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
delimiters: “ \”().”global pointerstart position
![Page 40: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/40.jpg)
A \0 \0 c o r p u s \0 \0 i s \0 a c o l l
e c t i o n o f t e x t s o f w
r e t t e n ( o r s p o k e n ) l
a n g u a g e p r e s e n t e d i n
e l e t r o n i c f o r m . \0
\0
delimiters: “ \”().”global pointerstart position
![Page 41: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/41.jpg)
Return address of start position.
“a”
![Page 42: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/42.jpg)
After 5 minutes...
![Page 43: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/43.jpg)
delimiters: “ \”().”
A \0 \“ c o r p u s \” i s a c o l l
e c t i o n \0 o f \0 t e x t s \0 o f \0 w
r e t t e n \0 \0 o r \0 s p o k e n \0 \0 l
a n g u a g e \0 p r e s e n t e d \0 i n
\0 e l e t r o n i c \0 f o r m . \0
global pointerstart position
\0 \0 \0 \0 \0
![Page 44: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/44.jpg)
delimiters: “ \”().”
A \0 \“ c o r p u s \” i s a c o l l
e c t i o n \0 o f \0 t e x t s \0 o f \0 w
r e t t e n \0 \0 o r \0 s p o k e n \0 \0 l
a n g u a g e \0 p r e s e n t e d \0 i n
\0 e l e t r o n i c \0 f o r m . \0
global pointerstart position
\0 \0 \0 \0 \0
\0
![Page 45: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/45.jpg)
Return address of start position.
“form”
![Page 46: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/46.jpg)
delimiters: “ \”().”
A \0 \“ c o r p u s \” i s a c o l l
e c t i o n \0 o f \0 t e x t s \0 o f \0 w
r e t t e n \0 \0 o r \0 s p o k e n \0 \0 l
a n g u a g e \0 p r e s e n t e d \0 i n
\0 e l e t r o n i c \0 f o r m . \0
global pointerstart position
\0 \0 \0 \0 \0
\0
![Page 47: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/47.jpg)
Return address of start position.
NULL
![Page 48: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/48.jpg)
• include <cstring> or <string.h>
• first time use string variable as parameter to setup global pointer
• others use NULL as parameter to avoid changing global pointer
• strtok will modify the original string
•whitespace character (\n, \r, \t ...)
![Page 50: [ACM-ICPC] About I/O](https://reader033.fdocument.pub/reader033/viewer/2022042513/555603dcd8b42a3f168b4831/html5/thumbnails/50.jpg)
Thank You for Your Listening.