1+ //
2+ // OJ#418.cpp
3+ // LeeteCodeOJ#418
4+ //
5+ // Created by Wang Yi on 25/10/16.
6+ // Copyright (c) 2016 Wang Yi. All rights reserved.
7+ //
8+
9+ #include " OJ#418.h"
10+ #include < string.h>
11+ #include < iomanip>
12+ #include < iostream>
13+ #define FOR (START, END ) for ((START )=0 ;(START )<(END );(START )+=1 ) {
14+ #define END ;}
15+ #define N 100
16+
17+ // first brute force solution
18+ int SentenceScreenFitting (char stc[][M], int row, int col, size_t l)
19+ {
20+ int i, j, t, s, k=0 , ret=0 ;
21+ int len[N] = {0 };
22+ FOR (i, l) len[i] = (int )strlen (stc[i]) END
23+
24+ FOR (i, row)
25+ j = col;
26+ t = k;
27+ while (j - len[k] >= 0 ) {
28+ j -= len[k] + 1 ;
29+ if (k+1 > l-1 ){
30+ FOR (s, k-t) std::cout << stc[t+s] << ' ' END ;
31+ std::cout << stc[t+s];
32+ if (j - len[0 ] >= 0 )
33+ std::cout << ' ' ;
34+ k = 0 ;
35+ t = 0 ;
36+ ret++;
37+ } else {
38+ k++;
39+ }
40+ }
41+ FOR (s, k-t-1 ) std::cout << stc[t+s] << ' ' END ;
42+ if (k-t > 0 )
43+ std::cout << stc[t+s];
44+
45+ std::cout << std::setfill (' -' ) << std::setw (j == col ? col:j+1 ) << " " << std::endl;
46+ END
47+
48+ return ret;
49+ }
50+
51+ /*
52+ int Solution::wordsTyping(vector<string>& sentence, int rows, int cols)
53+ {
54+ int i, j, t, s, k=0, ret=0, l=(int)sentence.size();
55+ int len[N] = {0};
56+ FOR(i, l) len[i] = (int)sentence[i].size() END
57+
58+ FOR(i, rows)
59+ j = cols;
60+ t = k;
61+ while (j - len[k] >= 0) {
62+ j -= len[k] + 1;
63+ if (k+1 > l-1){
64+ // FOR(s, k-t) std::cout << sentence[t+s] << ' ' END;
65+ // std::cout << sentence[t+s];
66+ // if (j - len[0] >= 0)
67+ // std::cout << ' ';
68+ k = 0;
69+ t = 0;
70+ ret++;
71+ } else{
72+ k++;
73+ }
74+ }
75+ // FOR(s, k-t-1) std::cout << sentence[t+s] << ' ' END;
76+ // if (k-t > 0)
77+ // std::cout << sentence[t+s];
78+ //
79+ // std::cout << std::setfill ('-') << std::setw(j == cols ? cols:j+1) << "" << std::endl;
80+ END
81+
82+ return ret;
83+ }
84+ */
85+
86+ // second boosting
87+ int Solution::wordsTyping (vector<string>& sentence, int rows, int cols)
88+ {
89+ int i, j, k=0 , ret=0 , l=(int )sentence.size ();
90+ // int t, s;
91+ int jump = 0 ;
92+ int len[N] = {0 };
93+ FOR (i, l) len[i] = (int )sentence[i].size ();jump += len[i] END
94+ jump += l;
95+
96+ FOR (i, rows)
97+ j = cols;
98+ // t = k;
99+
100+ if (k == 0 ){
101+ ret += j / jump;
102+ j = j % jump;
103+ }
104+
105+ while (j - len[k] >= 0 ) {
106+ j -= len[k] + 1 ;
107+ if (k+1 > l-1 ){
108+ // FOR(s, k-t) std::cout << sentence[t+s] << ' ' END;
109+ // std::cout << sentence[t+s];
110+ // if (j - len[0] >= 0)
111+ // std::cout << ' ';
112+ k = 0 ;
113+ // t = 0;
114+ ret += j / jump + 1 ;
115+ j = j % jump;
116+ } else {
117+ k++;
118+ }
119+ }
120+ // FOR(s, k-t-1) std::cout << sentence[t+s] << ' ' END;
121+ // if (k-t > 0)
122+ // std::cout << sentence[t+s];
123+ //
124+ // std::cout << std::setfill ('-') << std::setw(j == cols ? cols:j+1) << "" << std::endl;
125+ END
126+
127+ return ret;
128+ }
0 commit comments