CSES - HIIT Open 2019 - Results
Submission details
Task:Insert Whitespace
Sender:bits pls
Submission time:2019-05-25 15:19:53 +0300
Language:Python3
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3--details
#4--details
#5--details
#6--details
#7--details
#8--details
#9--details
#10--details
#11--details
#12--details
#13--details

Code

from copy import deepcopy
#n,h,W,p = 2,6,33,5
n,h,W,p = map(int, input().split())

s = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.""".split("\n")

def maplines(l, li=0, o=[], s=0):
	if len(l) == li: return [o]

	out = []
	for k in l[li].keys():
		lenv = s + k
		if lenv < h and h - lenv >= 2 or lenv == h:
			o1 = deepcopy(o)
			o1.append(k)
			for a in maplines(l, li+1, o1, lenv):
				out.append(a)
		elif lenv > h and lenv%h >= 2:
			lenv = lenv - lenv//h * h
			o1 = deepcopy(o)
			o1.append(k)
			for a in maplines(l, li+1, o1, lenv):
				out.append(a)

	return out
		
		

def addsp(w, wi=0, l=None, s=p):
	#if l == None: l = [p]
	if wi == len(w): return [l]

	l.append(w[wi])
	s += w[wi]
	wi += 1
	if s == W or wi == len(w): 
		l.append(0)
		return addsp(w, wi, l, s=0)
	elif s > W: return []

	l2 = deepcopy(l)
	s2 = s
	l.append(1)
	s += 1
	if s == W: 
		l.append(0)
		return addsp(w, wi, l, s=0)
	l2.append(2)
	s2 += 2

	lo = []	
	for add in addsp(w, wi, l, s):
		lo.append(add)
	if s2 == W:
		l2.append(0)
	for add in addsp(w, wi, l2, s2):
		lo.append(add)
	return lo
	

def getout(l):
	global dl
	o = []
	a = ""
	for i in range(len(dl)):
		o.append(dl[i][l[i]])
#	print(o)
	linecount = 0
	for i in range(len(o)):
		a += o[i].pop(0)*" "
		isword = True
		wordi = 0
		for el in o[i]:
			if el == 0:
				a += "\n"
				isword = True
				linecount += 1
				if linecount == h:
					linecount = 0
					a += "#\n"
				continue
			if isword:
				a += prgs[i][wordi]
				wordi += 1
			else:
				a += el * " "
			isword = not isword
	return a
			


prgs = []
dl = []
for _ in range(n):
	prg = input().split()
#	prg = s.pop(0).split()

	prgs.append(prg)
#	print("\n\n") 
#	print(prg)
	pgrl = list(map(len, prg))
#	print(pgrl)
	pos = addsp(pgrl, l=[p])
#	print(pos)
#	print(p)
#	input()
#	if pos == []:
#		print("IMPOSSIBLE")

	d = {}
	for P in pos:
		if len(P) not in d:
			d[len(P)] = P
	dl.append(d)
ans = maplines(dl)
if ans == []:
	print("IMPOSSIBLE")
else:
	print(getout(ans[0]), end="")
#print(ans)

	
	

	
	

Test details

Test 1

Verdict:

input
3 5 10 2
a a a a a a a a a a a a a a a ...

correct output
  a  a a a
a  a a a a
a  a a a a
a  a a a a
a
...

user output
(empty)

Test 2

Verdict:

input
3 6 10 2
a a a a a a a a a a a a a a a ...

correct output
IMPOSSIBLE

user output
(empty)

Test 3

Verdict:

input
3 7 10 2
a a a a a a a a a a a a a a a ...

correct output
IMPOSSIBLE

user output
(empty)

Test 4

Verdict:

input
3 8 10 2
a a a a a a a a a a a a a a a ...

correct output
  a  a a a
a  a a a a
a  a a a a
a  a a a a
a
...

user output
(empty)

Test 5

Verdict:

input
5 10 80 4
Lorem ipsum dolor sit amet, co...

correct output
    Lorem ipsum dolor sit amet...

user output
(empty)

Test 6

Verdict:

input
5 10 70 4
Lorem ipsum dolor sit amet, co...

correct output
    Lorem ipsum dolor sit amet...

user output
(empty)

Test 7

Verdict:

input
5 10 60 4
Lorem ipsum dolor sit amet, co...

correct output
IMPOSSIBLE

user output
(empty)

Test 8

Verdict:

input
55 20 80 4
Lorem ipsum dolor sit amet, co...

correct output
    Lorem  ipsum dolor sit ame...

user output
(empty)

Test 9

Verdict:

input
55 20 80 6
Lorem ipsum dolor sit amet, co...

correct output
      Lorem  ipsum  dolor  sit...

user output
(empty)

Test 10

Verdict:

input
55 20 80 8
Lorem ipsum dolor sit amet, co...

correct output
IMPOSSIBLE

user output
(empty)

Test 11

Verdict:

input
3 43 37 34
aaa aaaaa aaaa a aa aa aa aaa ...

correct output
                              ...

user output
(empty)

Test 12

Verdict:

input
4 50 73 12
aaaaa aa a aaa a aaa aaaa aaaa...

correct output
            aaaaa  aa  a  aaa ...

user output
(empty)

Test 13

Verdict:

input
4 6 62 31
aa aaa aaa aa aaaaa aaa aaaaa ...

correct output
                              ...

user output
(empty)