package chelper; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; import io.InputReader; import io.OutputWriter; import misc.SimpleSavingChelperSolution; public class D extends SimpleSavingChelperSolution { public void solve(int testNumber, InputReader in, OutputWriter out) { wrapSolve(testNumber, in, out); } @SuppressWarnings({"unchecked", "rawtypes"}) class Tree { Node root = new Node(); public int add(char[] s) { int cheatStart = 1; int cheatEnd = s.length; boolean typedNew = false; boolean canCheat = true; Node node = root; node.children.putIfAbsent(s[0], new Node()); node = node.children.get(s[0]); for (int i = 1; i < s.length; i++) { char c = s[i]; if (node.children.containsKey(c)) { if (node.children.size() > 1 || node.finishedHere) { cheatStart = i + 1; } } else { if (!typedNew) { typedNew = true; canCheat = node.children.size() == 0; cheatEnd = i; } node.children.put(c, new Node()); } node = node.children.get(c); } canCheat &= node.children.size() == 0; node.finishedHere = true; int total = cheatStart + Math.min(cheatEnd - cheatStart, 1) + s.length - cheatEnd; if (!canCheat) { total = s.length; } debug.println(); debug.printlns(new String(s)); debug.println(canCheat); debug.printlns(cheatStart, cheatEnd, total, '|', s.length - total); return total; } } class Node { Map children = new HashMap<>(); boolean finishedHere = false; } @Override public void solve(int testNumber) { int ans = 0; List words = new ArrayList<>(); int nonWordLength = 0; Pattern pattern = Pattern.compile("\\w+"); while (true) { String line = in.nextLine(); if (line == null) { break; } int l = line.length(); Matcher matcher = pattern.matcher(line); while (matcher.find()) { l -= matcher.end() - matcher.start(); words.add(matcher.group()); } nonWordLength += l + 1; } int totalLength = nonWordLength; Tree tree = new Tree(); for (String word : words) { int add = tree.add(word.toCharArray()); totalLength += add; } out.println(totalLength); } }