-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
73 lines (70 loc) · 1.85 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define fi first
#define se second
#define U unsigned
#define P std::pair
#define Re register
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(Re int i = a;i <= b;++i)
#define ROF(i,a,b) for(Re int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 1e6 + 5;
int n,r,c;
std::string str[MAXN];
int nxt[MAXN],len[MAXN],sum[MAXN];
int f[MAXN][21];
inline int calc(int i,int j){
return sum[j]-sum[i-1]+(j-i);
}
int main(){
std::cin >> n >> r >> c;
FOR(i,1,n) std::cin >> str[i],len[i] = str[i].length(),sum[i] = sum[i-1]+len[i];
int p = 1;
FOR(i,1,n){
if(len[i] > c){
f[i][0] = 0;continue;
}
while(p < n && calc(i,p+1) <= c) p++;
f[i][0] = p+1;
}
FOR(j,1,20) FOR(i,1,n) f[i][j] = f[f[i][j-1]][j-1];
int al=1,ar=0;
FOR(i,1,n){
int t = r,pos = i;
ROF(j,20,0){
if(f[pos][j] && t >= (1<<j)){
pos = f[pos][j];t -= (1<<j);
}
}
if(pos-i > ar-al+1) al = i,ar = pos-1;
}
int cnt = 0,tot = 1;
std::string out;
FOR(i,al,ar){
if(!out.empty() && out.length() + len[i] + 1 > c){
std::cout << out << std::endl;
out.clear();
}
if(out.empty()) out += str[i];
else if(!out.empty() && out.length()+len[i]+1 <= c) out += " " + str[i];
}
if(!out.empty()) std::cout << out << std::endl;
return 0;
}