SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
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.

Más contenido relacionado

Similar a [ACM-ICPC] About I/O

Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207Jay 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 2011swatith
 
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 185Mahmoud 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 210Mahmoud 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 storyAerospike
 
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 codeWim Godden
 
CSS parsing: performance tips & tricks
CSS parsing: performance tips & tricksCSS parsing: performance tips & tricks
CSS parsing: performance tips & tricksRoman 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 concurrencyMosky 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, 2016Chris Fregly
 
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 184Mahmoud Samir Fayed
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanWei-Yuan Chang
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Pythonkwatch
 
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 developersJim Roepcke
 

Similar a [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
 

Más de Chih-Hsuan Kuo

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-selectChih-Hsuan Kuo
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in RustChih-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 & 36Chih-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 GeckoChih-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 OSChih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 

Más de 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
 

Último

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Último (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 

[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.