fork download
  1. #include <string.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define MAX_WORD_LEN 20
  5. #define MAX_LINE_LEN 50
  6. int IsWhiteSpace(int );
  7. int ReadWord(char *word ) ;
  8. void AddWord(const char *word,char* , int* );
  9. void WriteLine(const char *line,int lineLend ,int numWords);
  10. void ClearLine(char *, int *lineLend,int *numWords);
  11.  
  12.  
  13. int main() {
  14. char word[MAX_WORD_LEN+1];
  15. int wordLend = 0;
  16. char line[MAX_LINE_LEN +1];
  17. int lineLend = 0;
  18. int numWords = 0;
  19. //<xóa dòng>
  20. ClearLine(line,&lineLend,&numWords);
  21. for(int cnt=0; cnt=100; cnt++) {
  22. //<Đọc một từ>
  23. wordLend = ReadWord(word) ;
  24. printf("%s\n", word);
  25. //if(Hết từ)
  26. if (wordLend == 0) {
  27. if (lineLend>0)
  28. // in dòng đó ra và thoát
  29. puts(line);
  30. return 0;
  31. }
  32. /*
  33.   <Từ không vừa dòng hiện tại >
  34.   <In dòng có căn phải>
  35.   <Xóa dòng>
  36.   */
  37. if((wordLend + 1 + lineLend) > MAX_LINE_LEN) {
  38. WriteLine(line,lineLend,numWords);
  39. ClearLine(line,&lineLend,&numWords);
  40. //fflush(stdin);
  41. }
  42. //<Thêm từ vào dòng>
  43. AddWord(word,line,&lineLend);
  44. numWords++;
  45. }
  46. return 0;
  47. }
  48. // đọc mọt từ
  49. int ReadWord(char *word) {
  50. int ch; // nếu là char thì không kiểm tra được kí tự EOF = 255
  51. int pos = 0;
  52.  
  53. /*
  54.   + bỏ qua white_space
  55.   + nếu đọc vào kí tự trăng ,tab nó sẽ bỏ qua để đọc kí tự tiếp theo ,xuống dòng
  56.   */
  57. ch = getchar();
  58. while(IsWhiteSpace(ch))
  59. ch = getchar();
  60. /* Lưu các ký tự vào đến MAX_WORK_LEN. */
  61. while ((!IsWhiteSpace(ch))&&( ch != 'EOF')) {
  62. if( pos < MAX_WORD_LEN) {
  63. word[pos] = (char)ch ;
  64. pos++;
  65. }
  66. ch = getchar();
  67. }
  68. word[pos] = '\0';
  69. // trả vể độ dài từ
  70. return pos;
  71. }
  72. int IsWhiteSpace(int ch) {
  73. return (( ch == ' ' )||( ch == '\n')||( ch == '\t'));
  74. }
  75. void AddWord(const char *word, char *line, int *lineLend) {
  76. if(*lineLend > 0) {
  77. line[*lineLend] = ' ';
  78. line[*lineLend + 1] = '\0';
  79. (*lineLend)++;
  80. }
  81. strcat(line,word);
  82. *lineLend += strlen(word);
  83. }
  84. void WriteLine(const char* line, int lineLend,int numWords) {
  85. int extraSpaces , spacesToInsert,i,j ;
  86. //<tính số khoảng tráng dư thừa cho dòng>
  87. extraSpaces = MAX_LINE_LEN - lineLend ;
  88. if(extraSpaces == 0)
  89. puts(line);
  90. else {
  91. for(i = 0 ; i < lineLend ; i++) {
  92. if(line[i] != ' ')
  93. //<printf charater>
  94. putchar(line[i]);
  95. else {
  96. //<tính số khoảng trống cần bù thêm>
  97. spacesToInsert = extraSpaces/(numWords - 1);
  98. //<In 1 space + thêm các space cần bù>
  99. for(j = 1 ; j <= spacesToInsert + 1; j++)
  100. putchar(' ');
  101. //<Giảm thêm không gian và đếm số từ>
  102. extraSpaces -= spacesToInsert;
  103. numWords--;
  104. }
  105. }
  106. }
  107. putchar('\n');
  108. }
  109. // đang lỗi ở đây:
  110. void ClearLine(char *line, int *lineLend,int *numWords) {
  111. line[0] = '\0';
  112. *lineLend = 0;
  113. *numWords = 0;
  114. }
Time limit exceeded #stdin #stdout 5s 9416KB
stdin
dou1 zi2 jan1 nei5 taai3 hou2 zaau2 bat1 dou3 jing13 zau2 teoi3 lou6 ngo5 jiu3 zeon3 ji5 mou4 heoi3 lou6 zeon3 teoi3 ngo5 bat1 zi1 dim2 syun3 hou2 bat1 zi1 dim2 syun3 hou2 sam1 zung1 zi2 gam2 cou3 bou6 ngo5 ci5 tiu3 zeon3 baat3 zan6 tou4 mou4 wu4 dei6 taam3 tou2 dik1 zoek6/ jit6 leoi6 jing4 zeon6 tou3 sam1 zung1 si1 si1 oi3 mou6 ngo5 ji5 deoi3 nei5 heoi3 zeon6 liu5 sam1 seoi3 naan4/naan6 bou2 daan6 si6 jin6 zoi6 waan4 mei6 dou3 seoi1 zak1 sam1 sing1/seng1 gei3 gwaa3 hou4/hou6 ngo5 zung3/zung1 ji5 hou3 zeon6 liu5 ngo5 mun5 sam1 oi3 mou6 si6 oi3 mou6 oi3 mou6 giu3 ngo5 sam1 tung3 fu2 oi3 mou6 oi3 mou6 daat6 dou3 fung1 din1 cing4 dou6
stdout
Standard output is empty