0w1

CFR 44 E. Anfisa the Monkey ( Greedy )

Problem - 44E - Codeforces

題意:
給 K, A, B,以及一個字串 S。要求輸出方案,使得將 S 分為 K 個子字串,滿足所有子字串長度在 [ A, B ] 之間。

資料規模:
The first line contains three integers k, a and b (1 ≤ k ≤ 200, 1 ≤ a ≤ b ≤ 200). The second line contains a sequence of lowercase Latin letters — the text typed by Anfisa. It is guaranteed that the given line is not empty and its length does not exceed 200 symbols.

解法:
考慮當前為處理的長度為 len,那麼至少要分成 ceil( len / B ),至多要分成 floor( len / A ) 塊。只要確定著需要分的數量符合當階段處理後的長度做即可。

時間 / 空間複雜度:
O( | S | )

K, A, B = map( int, input().split() )
S = input()

L = ( len( S ) + B - 1 ) // B
R = len( S ) // A

if not ( L <= K and K <= R ):
  exit( print( "No solution" ) )

ans = []
ptr = 0
while ptr < len( S ):
  nxt = ptr + A
  while K - ( len( ans ) + 1 ) < ( len( S ) - nxt + B - 1 ) // B:
    nxt += 1
  ans.append( S[ ptr : nxt ] )
  ptr = nxt

print( '\n'.join( ans ) )