[acm-icpc] about i/o

50
About I/O 郭至軒(KuoE0[email protected] KuoE0.ch

Upload: chih-hsuan-kuo

Post on 15-May-2015

458 views

Category:

Education


7 download

TRANSCRIPT

Page 3: [ACM-ICPC] About I/O

Standard Input & Output

- 標準輸入- 標準輸出

Page 4: [ACM-ICPC] About I/O

Standard Input & Output

- 標準輸入- 標準輸出

Page 5: [ACM-ICPC] About I/O

Standard Input & Output

- 標準輸入- 標準輸出

Page 6: [ACM-ICPC] About I/O

標準輸入→由鍵盤輸入標準輸出→由螢幕輸出

Page 7: [ACM-ICPC] About I/O

input file output file

Page 8: [ACM-ICPC] About I/O

Multiple Test Case

1 23 45 67 8

371115

input file output file

Page 9: [ACM-ICPC] About I/O

Continuous Processing

read one test caseno datastart

end

output result calculate

Page 10: [ACM-ICPC] About I/O

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

End Of File若題目未指定測資終止條件,則為判斷 EOF 作為終止

條件!

scanf fgets cin

while (scanf() != EOF){

...}

while (fgets() != 0){

...}

while (cin >> x){

...}

Page 12: [ACM-ICPC] About I/O

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

If you write the code like previous page in contest, you

will lose the contest.

Page 14: [ACM-ICPC] About I/O

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

I/O Performance

Page 16: [ACM-ICPC] About I/O

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

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

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

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

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

Stream I/O is Still Slow

Recommend to use C I/O.

Page 22: [ACM-ICPC] About I/O

Buffered Technique

fgets/puts are faster than scanf/printf.Base on fgets/puts function.

Page 23: [ACM-ICPC] About I/O

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

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

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

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

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

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

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

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

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

Return address of start position.

“A”

Page 33: [ACM-ICPC] About I/O

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

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

Return address of start position.

“corpus”

Page 36: [ACM-ICPC] About I/O

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

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

Return address of start position.

“is”

Page 39: [ACM-ICPC] About I/O

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

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

Return address of start position.

“a”

Page 42: [ACM-ICPC] About I/O

After 5 minutes...

Page 43: [ACM-ICPC] About I/O

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

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

Return address of start position.

“form”

Page 46: [ACM-ICPC] About I/O

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

Return address of start position.

NULL

Page 48: [ACM-ICPC] About I/O

• 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 49: [ACM-ICPC] About I/O

POJ 3981 - 字符串替换

Practice Now

Page 50: [ACM-ICPC] About I/O

Thank You for Your Listening.