USACO Bronze 2020 January - Word Processor

Authors: Danh Ta Chi Thanh, Juheon Rhee, Jeffrey Meng

Official Analysis (Python)

Video Solution

By Jeffrey Meng

Video Solution Code

Implementation

C++

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
freopen("word.in", "r", stdin);
freopen("word.out", "w", stdout);

Java

import java.io.*;
import java.util.*;
public class Word {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new FileReader("word.in"));
StringTokenizer initial = new StringTokenizer(read.readLine());
initial.nextToken(); // we won't need N
int maxWidth = Integer.parseInt(initial.nextToken());

Python

with open("word.in") as read:
max_width = int(read.readline().split()[1])
essay = read.readline().split()
formatted = ""
rn_length = 0
for w in essay:
# ok, this word exceeds the limit, let's wrap it around
if rn_length + len(w) > max_width:
formatted = formatted[:-1] + "\n"

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!