Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[ACM-ICPC] About I/O

KuoE0
February 20, 2013

[ACM-ICPC] About I/O

KuoE0

February 20, 2013
Tweet

More Decks by KuoE0

Other Decks in Programming

Transcript

  1. Multiple Test Case 1 2 3 4 5 6 7

    8 3 7 11 15 input file output file
  2. read: 1 2 start calculate write: 3 read: 3 4

    calculate write: 7 read: 5 6 calculate write: 11 read: 7 8 calculate write: 15 end
  3. End Of File ए୊໨ະࢦఆଌࢿऴࢭᑍ݅ɼଇҝ൑Ꮧ EOF ࡞ҝऴࢭ ᑍ݅ʂ scanf fgets cin

    while (scanf() != EOF) { ... } while (fgets() != 0) { ... } while (cin >> x) { ... }
  4. File Input & Output fopen 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();
  5. freopen redirect the file I/O to standard I/O freopen(“inputfile”, “r”,

    stdin); freopen(“output”, “w”, stdout); scanf(...); printf(...);
  6. 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
  7. 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
  8. Improve Stream I/O Stream 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,
  9. 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 cin cin without sync
  10. 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 cout cout without sync
  11. Buffered Read Read mass data, and parse by self. char

    buf[ 1000 ]; int a, b; fgets( buf, sizeof( buf ), stdin ); sscanf( buf, “%d %d”, &a, &b );
  12. 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.
  13. 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 cin cin without sync fgets
  14. 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 cout cout without sync puts
  15. Advanced Parsing Skill char* strtok( char *str, const char *delimiters

    ); strtok Split string into tokens. str: ཉ੾ׂ೭ࣈ۲ delimiters: ෼ִࣈූࣈ۲ return value: ࢦ޲ᙛલ੾ׂࣈ۲೭ࢦ ඪɼए੾ׂ׬ඟଇճၚ NULL
  16. 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 a collection of texts of written or spoken language presented in electronic form
  17. 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 ); }
  18. 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 pointer start position
  19. 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 pointer start position
  20. 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 pointer start position
  21. 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 pointer start position
  22. 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 pointer start position
  23. 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 pointer start position
  24. 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 pointer start position
  25. 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 pointer start position
  26. 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 pointer start position \0 \0 \0 \0 \0
  27. 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 pointer start position \0 \0 \0 \0 \0 \0
  28. 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 pointer start position \0 \0 \0 \0 \0 \0
  29. •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 ...)