Skip to content

Instantly share code, notes, and snippets.

@malkoG
Created April 5, 2019 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malkoG/f06180d181b6ae61ad819cb6cddc691d to your computer and use it in GitHub Desktop.
Save malkoG/f06180d181b6ae61ad819cb6cddc691d to your computer and use it in GitHub Desktop.
noj.am/9935
import java.util.*
fun main(args: Array<String>) = with(Scanner(System.`in`)) {
var state_stack = Stack<Int>()
val text = nextLine()
val pattern = nextLine()
var cursor = 0
var result = StringBuilder()
for (i in 0..text.length-1) {
val ch = text[i]
result.append(ch)
val txt_size = result.length
if (ch == pattern[cursor]) {
cursor += 1
} else {
if (ch == pattern[0]) {
state_stack.push(cursor)
cursor = 1
} else {
state_stack = Stack<Int>()
cursor = 0
}
}
if (cursor == pattern.length) {
result.delete(txt_size - cursor, txt_size)
cursor = if (state_stack.size > 0) state_stack.pop() else 0
}
}
println(if (result.length == 0) "FRULA" else result.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment