USACO Bronze 2021 January - Uddered but not Herd

Authors: Ananth Kashyap, Sofia Yang


Official Analysis

Implementation

Time Complexity: O(word)\mathcal{O}(|\texttt{word}|)

Python

# Take in the cowphabet and string as strings through standard Python input
cowphabet = input()
word = input()
# For any string, we always have to look through the cowphabet at least once.
num_times = 1
for i in range(len(word) - 1):
# Check if next character in string appears before previous character
if cowphabet.index(word[i + 1]) <= cowphabet.index(word[i]):
num_times += 1
print(num_times)

Java

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 numTimes = 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!