CSES - HIIT Open 2019 - Results
Submission details
Task:Insert Whitespace
Sender:bits pls
Submission time:2019-05-25 15:18:37 +0300
Language:Python3
Status:READY
Result:
Test results
testverdicttime
#10.05 sdetails
#20.06 sdetails
#30.05 sdetails
#40.06 sdetails
#50.06 sdetails
#60.06 sdetails
#70.06 sdetails
#80.06 sdetails
#90.06 sdetails
#100.05 sdetails
#110.06 sdetails
#120.05 sdetails
#130.06 sdetails

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 5

Verdict:

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

correct output
    Lorem ipsum dolor sit amet...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 6

Verdict:

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

correct output
    Lorem ipsum dolor sit amet...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 7

Verdict:

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

correct output
IMPOSSIBLE

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 8

Verdict:

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

correct output
    Lorem  ipsum dolor sit ame...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 9

Verdict:

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

correct output
      Lorem  ipsum  dolor  sit...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 10

Verdict:

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

correct output
IMPOSSIBLE

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 11

Verdict:

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

correct output
                              ...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined

Test 13

Verdict:

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

correct output
                              ...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 104, in <module>
    pos = addsp(pgrl, l=[p])
  File "input/code.py", line 37, in addsp
    if s == W or wi == len(w): 
NameError: name 'W' is not defined