Official Analysis (C++, Python)
Explanation
We know that Bessie hums the cowphabet in a fixed order. Based on this, we iterate over consecutive pairs of letters in Farmer John's word. If the current letter appears before the next letter in the cowphabet, there is no need for Bessie to start a new hum (i.e., a new cycle); otherwise, we increment the number of times we have hummed the alphabet ().
Implementation
Time Complexity:
import java.io.*;public class UdderedButNotHerd {public static void main(String[] args) throws IOException {BufferedReader input = new BufferedReader(new InputStreamReader(System.in));String cowphabet = input.readLine();char[] word = input.readLine().toCharArray();int humTimes = 1; // There must be at least one humming sequence.for (int i = 0; i < word.length - 1; i++) {
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!