#! /usr/bin/python3 # Last edited on 2026-04-21 17:23:59 by stolfi import sys, os, re from sys import stdin as inp, stdout as out, stderr as err import random; from random import random as rand def main(alg,max_tlen): # Reads a stream of tokens, one per line. # Typesets them into lines of at most {max_tlen} letters # using the trivial line-breaking algorithm. random.seed(4617) Pabbr0 = 0.02 # Prob of spontaneous abbreviation in SLA. Pabbr1 = 0.50 # Prob of forced abbreviation in SLA. L = [] nL = 0 for word in inp: word = word.strip() if alg == "SLA" and word[-3:] == 'iin': # Simulate spurious abbreviation: if rand() < Pabbr0: word = word[0:-3] + "m" nL1 = nL + 1 + len(word) if nL1 > max_tlen and alg == "SLA" and word[-3:] == 'iin': # Try areviating {word} to fit: word2 = word[0:-3] + "m" nL2 = nL + 1 + len(word2) if nL2 <= max_tlen: word = word2 # Trivial line breaking: nL1 = nL + 1 + len(word) if nL1 > max_tlen: out.write(" ".join(L)); out.write("\n") L = [ word, ]; nL = len(word) else: L.append(word); nL = nL1 out.flush() # ---------------------------------------------------------------------- iarg = 1 alg = sys.argv[iarg] ; iarg += 1 max_tlen = int(sys.argv[iarg]) ; iarg += 1 main(alg, max_tlen)