USACO Bronze 2015 February - Censoring
Authors: Brad Ma, Ahmad Bilal, Amogha Pokkulandra
Explanation
Let us define as the final censored text.
We can iterate through every character of , appending it to . However, whenever we append a character we we need to check if ends with the censored word. If it does, we remove it from by taking off the last few characters.
As a demonstration, let's try this on the example case where:
Our solution will loop through each character of , appending it to until it becomes , when it will cut off the last 3 characters since they equal . This results in becoming . Right after this, becomes as the next letter in is , so we omit the last 3 characters again and becomes .
After this, the check isn't triggered anymore, so we end up with as our final word.
Implementation
Time Complexity:
Python
import syssys.stdin = open("censor.in", "r")sys.stdout = open("censor.out", "w")s = input().strip()t = input().strip()censored = ""# Add each character to the censored string
C++
#include "bits/stdc++.h"using namespace std;Code Snippet: {USACO-style I/O. See General / Input & Output (Click to expand)int main() {setIO("censor");string s;string t;cin >> s >> t;
Java
import java.io.*;import java.util.*;public class Censor {public static void main(String[] args) throws IOException {Kattio io = new Kattio("censor");String s = io.next();String t = io.next();// We use StringBuilder in Java because it's significantly faster
Video Solution
By Amogha Pokkulandra
Video Solution Code
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!