Upcoming SlideShare
×

772 views
611 views

Published on

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
772
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
34
0
Likes
1
Embeds 0
No embeds

No notes for slide

1. 1. About I/O 郭至軒（KuoE0） KuoE0.tw@gmail.com KuoE0.ch
2. 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. 3. Standard Input & Output - 標準輸入 - 標準輸出
4. 4. Standard Input & Output - 標準輸入 - 標準輸出
5. 5. Standard Input & Output - 標準輸入 - 標準輸出
6. 6. 標準輸入→由 盤輸入標準輸出→由螢幕輸出
7. 7. input ﬁle output ﬁle
8. 8. Multiple Test Case12 334 756 1178 15 input ﬁle output ﬁle
9. 9. Continuous Processing output calculate result read one start no data test case end
10. 10. start read: 1 2 calculate write: 3read: 5 6 write: 7 calculate read: 3 4calculate write: 11 read: 7 8 calculate end write: 15
11. 11. End Of File若題目未指定測資終止條件，則為判斷 EOF 作為終止 條件！ scanf fgets cinwhile (scanf() != EOF) while (fgets() != 0) while (cin >> x){ { { ... ... ...} } }
12. 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. 13. If you write the code likeprevious page in contest, you will lose the contest.
14. 14. freopen redirect the ﬁle I/O to standard I/Ofreopen(“inputfile”, “r”, stdin);freopen(“output”, “w”, stdout);scanf(...);printf(...);
15. 15. I/O Performance
16. 16. scanf & cin 010^4 0 0.0110^5 0.04 0.1410^6 0.41 1.2710^7 3.45 0 sec 1 sec 2 sec 3 sec 4 sec scanf cin
17. 17. printf & cout 010^4 0.01 0.0110^5 0.04 0.1110^6 0.42 1.0810^7 4.02 0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec printf cout
18. 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. 19. cin without sync 010^4 0 0 0.0110^5 0.04 0.02 0.1410^6 0.41 0.19 1.2710^7 3.45 1.70 0 sec 1 sec 2 sec 3 sec 4 sec scanf cin cin without sync
20. 20. cout without sync 010^4 0.01 0.01 0.0110^5 0.04 0.04 0.1110^6 0.42 0.36 1.0810^7 4.02 3.49 0 sec 1.25 sec 2.5 sec 3.75 sec 5 sec printf cout cout without sync
21. 21. Stream I/O is Still SlowRecommendto use C I/O.
22. 22. Buffered Technique Base on fgets/puts function.fgets/puts are faster than scanf/printf.
23. 23. 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 );
24. 24. Buffered WriteStore mass data into temporal buffer,and write them once.char buf[ 1000 ];int ret = a + b;sprintf( buf, “%d”, ret );puts( buf );
25. 25. Buffering Technique 0 010^4 0 0 0.01 0.0410^5 0.02 0.01 0.14 0.4110^6 0.19 0.05 1.27 3.4510^7 1.70 0.53 0 sec 1 sec 2 sec 3 sec 4 sec scanf cin cin without sync fgets
26. 26. Buffering Technique 0 0.0110^4 0.01 0 0.01 0.0410^5 0.04 0.02 0.11 0.4210^6 0.36 0.16 1.08 4.0210^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. 27. Advanced Parsing Skill strtok Split string into tokens.char* strtok( char *str, const char *delimiters ); str: 欲切割之字串 delimiters: 分隔字符字串 return value: 指向當前切割字串之指 標，若切割完畢則回傳 NULL
28. 28. strtok original string:A “corpus” is a collection of texts of written (orspoken) 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. 29. char str = “A “corpus” is a collection of texts ofwritten (or spoken) language presented in electronicform.”for ( char *token = strtok( str, “ ”().” ); token !=NULL; token = strtok( NULL, “ ”().” ) ) { puts( token );}
30. 30. start position delimiters: “ ”().” global pointerA “ c o r p u s ” i s a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 31. start position delimiters: “ ”().” global pointerA 0 “ c o r p u s ” i s a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 32. Return address of start position. “A”
33. 33. start position delimiters: “ ”().” global pointerA 0 “ c o r p u s ” i s a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 34. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 “ ” i s a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 35. Return address of start position. “corpus”
36. 36. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 i s a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 37. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 0 i s 0 a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 38. Return address of start position. “is”
39. 39. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 0 i s 0 a c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 40. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 0 i s 0 a 0 c o l le c t i o n o f t e x t s o f wr e t t e n ( o r s p o k e n ) la 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. 41. Return address of start position. “a”
42. 42. After 5 minutes...
43. 43. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 0 i “ ” s 0 a 0 c o l le c t i o n 0 o f 0 t e x t s 0 o f 0 wr e t t e n 0 0 o r 0 s p o k e n 0 0 la n g u a g e 0 p r e s e n t e d 0 i n0 e l e t r o n i c 0 f o r m . 0
44. 44. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 0 i “ ” s 0 a 0 c o l le c t i o n 0 o f 0 t e x t s 0 o f 0 wr e t t e n 0 0 o r 0 s p o k e n 0 0 la n g u a g e 0 p r e s e n t e d 0 i n0 e l e t r o n i c 0 f o r m 0 0 .
45. 45. Return address of start position. “form”
46. 46. start position delimiters: “ ”().” global pointerA 0 0 c o r p u s 0 0 i “ ” s 0 a 0 c o l le c t i o n 0 o f 0 t e x t s 0 o f 0 wr e t t e n 0 0 o r 0 s p o k e n 0 0 la n g u a g e 0 p r e s e n t e d 0 i n0 e l e t r o n i c 0 f o r m 0 0 .
47. 47. Return address of start position. NULL
48. 48. • include <cstring> or <string.h>• ﬁrst 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. 49. Practice NowPOJ 3981 - 字符串替换
50. 50. Thank You for Your Listening.