SlideShare a Scribd company logo
About I/O
   郭至軒(KuoE0)
  KuoE0.tw@gmail.com
       KuoE0.ch
Attribution-ShareAlike 3.0 Unported
           (CC BY-SA 3.0)

  http://creativecommons.org/licenses/by-sa/3.0/

              Latest update: Feb 27, 2013
Standard Input & Output




        - 標準輸入
        - 標準輸出
Standard Input & Output




        - 標準輸入
        - 標準輸出
Standard Input & Output




        - 標準輸入
        - 標準輸出
標準輸入→由   盤輸入
標準輸出→由螢幕輸出
input file   output file
Multiple Test Case

12               3
34               7
56               11
78               15
 input file         output file
Continuous Processing
         output
                   calculate
         result



                   read one
 start   no data
                   test case



          end
start     read: 1 2   calculate   write: 3

read: 5 6   write: 7    calculate   read: 3 4

calculate   write: 11   read: 7 8   calculate

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

        scanf                  fgets                   cin

while (scanf() != EOF) while (fgets() != 0)   while (cin >> x)
{                      {                      {
   ...                    ...                    ...
}                      }                      }
File Input & Output
               fopen                   fstream (C++ only)

                                 ifstream in =
FILE *in = fopen(“inputfile”);   ifstream(“inputfile”);
FILE *out =
fopen(“outputfile”);             ofstream out =
                                 ofstream(“outputfile”);
fscanf(in, ...);
fprintf(out, ...);               in >> x;
                                 out << x;
fclose(in);
fclose(out);                     in.close();
                                 out.close();
If you write the code like
previous page in contest, you
     will lose the contest.
freopen
 redirect the file I/O to standard I/O

freopen(“inputfile”, “r”, stdin);
freopen(“output”, “w”, stdout);

scanf(...);
printf(...);
I/O Performance
scanf & cin
       0
10^4
       0

       0.01
10^5
       0.04

           0.14
10^6
                  0.41

                                 1.27
10^7
                                                                3.45

   0 sec                 1 sec          2 sec           3 sec          4 sec

                                                scanf           cin
printf & cout
       0
10^4
       0.01

       0.01
10^5
       0.04

           0.11
10^6
                  0.42

                          1.08
10^7
                                                              4.02

   0 sec                 1.25 sec   2.5 sec        3.75 sec             5 sec

                                              printf             cout
Improve Stream I/O
                         Stream I/O need to keep
                         themselves in sync with
                         the underlying C library.

Add this line in code,
std::ios::sync_with_stdio(false);
                           if you won’t use C I/O.
cin without sync
       0
10^4   0
       0
       0.01
10^5    0.04
       0.02
           0.14
10^6               0.41
            0.19
                                  1.27
10^7                                                                   3.45
                                         1.70

   0 sec                  1 sec             2 sec            3 sec              4 sec

                                                    scanf                     cin
                                                    cin without sync
cout without sync
       0
10^4   0.01
       0.01
       0.01
10^5   0.04
       0.04
        0.11
10^6            0.42
               0.36
                        1.08
10^7                                                       4.02
                                                3.49

   0 sec               1.25 sec   2.5 sec       3.75 sec            5 sec

                                       printf                     cout
                                       cout without sync
Stream I/O is Still Slow

Recommend
to use C I/O.
Buffered Technique

       Base on fgets/puts function.
fgets/puts are faster than scanf/printf.
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 );
Buffered Write
Store mass data into temporal buffer,
and write them once.
char buf[ 1000 ];
int ret = a + b;
sprintf( buf, “%d”, ret );
puts( buf );
Buffering Technique
       0
       0
10^4   0
       0
       0.01
        0.04
10^5   0.02
       0.01
          0.14
                   0.41
10^6        0.19
        0.05
                                    1.27
                                                                     3.45
10^7                                       1.70
                     0.53

   0 sec                    1 sec             2 sec         3 sec           4 sec

                                                  scanf                 cin
                                                  cin without sync      fgets
Buffering Technique
       0
       0.01
10^4   0.01
       0
       0.01
        0.04
10^5    0.04
       0.02
         0.11
                0.42
10^6           0.36
           0.16
                        1.08
                                                                  4.02
10^7                                                   3.49
                                  1.71

   0 sec               1.25 sec          2.5 sec       3.75 sec            5 sec

                                              printf                     cout
                                              cout without sync          puts
Advanced Parsing Skill
           strtok       Split string into tokens.

char* strtok( char *str, const char *delimiters );

      str: 欲切割之字串
      delimiters: 分隔字符字串
      return value: 指向當前切割字串之指
      標,若切割完畢則回傳 NULL
strtok
                  original string:
A “corpus” is a collection of texts of written (or
spoken) language presented in electronic form.

                take out all words:
   A            corpus    is           a
   collection   of        texts        of
   written      or        spoken       language
   presented    in        electronic   form
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 );
}
start position
            delimiters: “ ”().”
                                         global pointer
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
start position
          delimiters: “ ”().”
                                       global pointer
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
Return address of start position.


            “A”
start position
          delimiters: “ ”().”
                                       global pointer
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
start position
          delimiters: “ ”().”
                                       global pointer
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
Return address of start position.


      “corpus”
start position
          delimiters: “ ”().”
                                       global pointer
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
start position
          delimiters: “ ”().”
                                     global pointer
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
Return address of start position.


            “is”
start position
          delimiters: “ ”().”
                                     global pointer
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
start position
          delimiters: “ ”().”
                                     global pointer
A 0 0 c o r p u s 0 0 i          s 0 a 0 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
Return address of start position.


            “a”
After 5 minutes...
start position
           delimiters: “ ”().”
                                     global pointer
A 0 0 c o r p u s 0 0 i
     “             ”              s 0 a 0 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
start position
           delimiters: “ ”().”
                                     global pointer
A 0 0 c o r p u s 0 0 i
     “             ”               s 0 a 0 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 0
                                            .
Return address of start position.


        “form”
start position
           delimiters: “ ”().”
                                     global pointer
A 0 0 c o r p u s 0 0 i
     “             ”               s 0 a 0 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 0
                                            .
Return address of start position.


         NULL
• 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 ...)
Practice Now
POJ 3981 - 字符串替换
Thank You for Your
    Listening.

More Related Content

Similar to [ACM-ICPC] About I/O

Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207
Jay Coskey
 
(Www.entrance exam.net)-ignou mca solved assignment 2011
(Www.entrance exam.net)-ignou mca  solved assignment 2011(Www.entrance exam.net)-ignou mca  solved assignment 2011
(Www.entrance exam.net)-ignou mca solved assignment 2011
swatith
 
The Ring programming language version 1.5.4 book - Part 25 of 185
The Ring programming language version 1.5.4 book - Part 25 of 185The Ring programming language version 1.5.4 book - Part 25 of 185
The Ring programming language version 1.5.4 book - Part 25 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 32 of 210
The Ring programming language version 1.9 book - Part 32 of 210The Ring programming language version 1.9 book - Part 32 of 210
The Ring programming language version 1.9 book - Part 32 of 210
Mahmoud Samir Fayed
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
Qiangning Hong
 
Golang Performance : microbenchmarks, profilers, and a war story
Golang Performance : microbenchmarks, profilers, and a war storyGolang Performance : microbenchmarks, profilers, and a war story
Golang Performance : microbenchmarks, profilers, and a war story
Aerospike
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
Wim Godden
 
CSS parsing: performance tips & tricks
CSS parsing: performance tips & tricksCSS parsing: performance tips & tricks
CSS parsing: performance tips & tricks
Roman Dvornov
 
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Edureka!
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrency
Mosky Liu
 
Gotcha! Ruby things that will come back to bite you.
Gotcha! Ruby things that will come back to bite you.Gotcha! Ruby things that will come back to bite you.
Gotcha! Ruby things that will come back to bite you.
David Tollmyr
 
Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)
Ontico
 
Advanced Spark and TensorFlow Meetup May 26, 2016
Advanced Spark and TensorFlow Meetup May 26, 2016Advanced Spark and TensorFlow Meetup May 26, 2016
Advanced Spark and TensorFlow Meetup May 26, 2016
Chris Fregly
 
Inside Winnyp
Inside WinnypInside Winnyp
Inside Winnyp
FFRI, Inc.
 
The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184
Mahmoud Samir Fayed
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
Wei-Yuan Chang
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
kwatch
 
Swift Ready for Production?
Swift Ready for Production?Swift Ready for Production?
Swift Ready for Production?
Crispy Mountain
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
Jim Roepcke
 
Python
PythonPython
Python
Wei-Bo Chen
 

Similar to [ACM-ICPC] About I/O (20)

Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207
 
(Www.entrance exam.net)-ignou mca solved assignment 2011
(Www.entrance exam.net)-ignou mca  solved assignment 2011(Www.entrance exam.net)-ignou mca  solved assignment 2011
(Www.entrance exam.net)-ignou mca solved assignment 2011
 
The Ring programming language version 1.5.4 book - Part 25 of 185
The Ring programming language version 1.5.4 book - Part 25 of 185The Ring programming language version 1.5.4 book - Part 25 of 185
The Ring programming language version 1.5.4 book - Part 25 of 185
 
The Ring programming language version 1.9 book - Part 32 of 210
The Ring programming language version 1.9 book - Part 32 of 210The Ring programming language version 1.9 book - Part 32 of 210
The Ring programming language version 1.9 book - Part 32 of 210
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
 
Golang Performance : microbenchmarks, profilers, and a war story
Golang Performance : microbenchmarks, profilers, and a war storyGolang Performance : microbenchmarks, profilers, and a war story
Golang Performance : microbenchmarks, profilers, and a war story
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
 
CSS parsing: performance tips & tricks
CSS parsing: performance tips & tricksCSS parsing: performance tips & tricks
CSS parsing: performance tips & tricks
 
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrency
 
Gotcha! Ruby things that will come back to bite you.
Gotcha! Ruby things that will come back to bite you.Gotcha! Ruby things that will come back to bite you.
Gotcha! Ruby things that will come back to bite you.
 
Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)
 
Advanced Spark and TensorFlow Meetup May 26, 2016
Advanced Spark and TensorFlow Meetup May 26, 2016Advanced Spark and TensorFlow Meetup May 26, 2016
Advanced Spark and TensorFlow Meetup May 26, 2016
 
Inside Winnyp
Inside WinnypInside Winnyp
Inside Winnyp
 
The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Swift Ready for Production?
Swift Ready for Production?Swift Ready for Production?
Swift Ready for Production?
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
Python
PythonPython
Python
 

More from Chih-Hsuan Kuo

Rust
RustRust
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
Chih-Hsuan Kuo
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
Chih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
Chih-Hsuan Kuo
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
Chih-Hsuan Kuo
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
Chih-Hsuan Kuo
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
Chih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
Chih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
Chih-Hsuan Kuo
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
Chih-Hsuan Kuo
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
Chih-Hsuan Kuo
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
Chih-Hsuan Kuo
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
Chih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
Chih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
Chih-Hsuan Kuo
 

More from Chih-Hsuan Kuo (20)

Rust
RustRust
Rust
 
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 

Recently uploaded

A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
shreyassri1208
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
Celine George
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
RandolphRadicy
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 

Recently uploaded (20)

A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 

[ACM-ICPC] About I/O

  • 1. About I/O 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  • 2. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ Latest update: Feb 27, 2013
  • 3. Standard Input & Output - 標準輸入 - 標準輸出
  • 4. Standard Input & Output - 標準輸入 - 標準輸出
  • 5. Standard Input & Output - 標準輸入 - 標準輸出
  • 6. 標準輸入→由 盤輸入 標準輸出→由螢幕輸出
  • 7. input file output file
  • 8. Multiple Test Case 12 3 34 7 56 11 78 15 input file output file
  • 9. Continuous Processing output calculate result read one start no data test case end
  • 10. start read: 1 2 calculate write: 3 read: 5 6 write: 7 calculate read: 3 4 calculate write: 11 read: 7 8 calculate end write: 15
  • 11. End Of File 若題目未指定測資終止條件,則為判斷 EOF 作為終止 條件! scanf fgets cin while (scanf() != EOF) while (fgets() != 0) while (cin >> x) { { { ... ... ... } } }
  • 12. File Input & Output fopen fstream (C++ only) ifstream in = FILE *in = fopen(“inputfile”); ifstream(“inputfile”); FILE *out = fopen(“outputfile”); ofstream out = ofstream(“outputfile”); fscanf(in, ...); fprintf(out, ...); in >> x; out << x; fclose(in); fclose(out); in.close(); out.close();
  • 13. If you write the code like previous page in contest, you will lose the contest.
  • 14. freopen redirect the file I/O to standard I/O freopen(“inputfile”, “r”, stdin); freopen(“output”, “w”, stdout); scanf(...); printf(...);
  • 16. scanf & cin 0 10^4 0 0.01 10^5 0.04 0.14 10^6 0.41 1.27 10^7 3.45 0 sec 1 sec 2 sec 3 sec 4 sec scanf cin
  • 17. printf & cout 0 10^4 0.01 0.01 10^5 0.04 0.11 10^6 0.42 1.08 10^7 4.02 0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec printf cout
  • 18. Improve Stream I/O Stream I/O need to keep themselves in sync with the underlying C library. Add this line in code, std::ios::sync_with_stdio(false); if you won’t use C I/O.
  • 19. cin without sync 0 10^4 0 0 0.01 10^5 0.04 0.02 0.14 10^6 0.41 0.19 1.27 10^7 3.45 1.70 0 sec 1 sec 2 sec 3 sec 4 sec scanf cin cin without sync
  • 20. cout without sync 0 10^4 0.01 0.01 0.01 10^5 0.04 0.04 0.11 10^6 0.42 0.36 1.08 10^7 4.02 3.49 0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec printf cout cout without sync
  • 21. Stream I/O is Still Slow Recommend to use C I/O.
  • 22. Buffered Technique Base on fgets/puts function. fgets/puts are faster than scanf/printf.
  • 23. 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 );
  • 24. Buffered Write Store mass data into temporal buffer, and write them once. char buf[ 1000 ]; int ret = a + b; sprintf( buf, “%d”, ret ); puts( buf );
  • 25. Buffering Technique 0 0 10^4 0 0 0.01 0.04 10^5 0.02 0.01 0.14 0.41 10^6 0.19 0.05 1.27 3.45 10^7 1.70 0.53 0 sec 1 sec 2 sec 3 sec 4 sec scanf cin cin without sync fgets
  • 26. Buffering Technique 0 0.01 10^4 0.01 0 0.01 0.04 10^5 0.04 0.02 0.11 0.42 10^6 0.36 0.16 1.08 4.02 10^7 3.49 1.71 0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec printf cout cout without sync puts
  • 27. Advanced Parsing Skill strtok Split string into tokens. char* strtok( char *str, const char *delimiters ); str: 欲切割之字串 delimiters: 分隔字符字串 return value: 指向當前切割字串之指 標,若切割完畢則回傳 NULL
  • 28. strtok original string: A “corpus” is a collection of texts of written (or spoken) language presented in electronic form. take out all words: A corpus is a collection of texts of written or spoken language presented in electronic form
  • 29. 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 ); }
  • 30. start position delimiters: “ ”().” global pointer 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
  • 31. start position delimiters: “ ”().” global pointer 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
  • 32. Return address of start position. “A”
  • 33. start position delimiters: “ ”().” global pointer 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
  • 34. start position delimiters: “ ”().” global pointer 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
  • 35. Return address of start position. “corpus”
  • 36. start position delimiters: “ ”().” global pointer 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
  • 37. start position delimiters: “ ”().” global pointer 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
  • 38. Return address of start position. “is”
  • 39. start position delimiters: “ ”().” global pointer 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
  • 40. start position delimiters: “ ”().” global pointer A 0 0 c o r p u s 0 0 i s 0 a 0 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
  • 41. Return address of start position. “a”
  • 43. start position delimiters: “ ”().” global pointer A 0 0 c o r p u s 0 0 i “ ” s 0 a 0 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
  • 44. start position delimiters: “ ”().” global pointer A 0 0 c o r p u s 0 0 i “ ” s 0 a 0 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 0 .
  • 45. Return address of start position. “form”
  • 46. start position delimiters: “ ”().” global pointer A 0 0 c o r p u s 0 0 i “ ” s 0 a 0 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 0 .
  • 47. Return address of start position. NULL
  • 48. • 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 ...)
  • 49. Practice Now POJ 3981 - 字符串替换
  • 50. Thank You for Your Listening.