struct Scanner { private var elements = [String]() private var index = 0 mutating func peek() -> String { while elements.count == index { elements = readLine()!.split(separator: " ").map(String.init) index = 0 } return elements[index] } mutating func next() -> String { defer { index += 1 } return peek() } mutating func nextInt() -> Int { return Int(next())! } mutating func nextInts(_ n: Int) -> [Int] { return (0 ..< n).map { _ in nextInt() } } mutating func nextDouble() -> Double { return Double(next())! } } struct Zomega: Hashable { let x: Int let y: Int init(_ x: Int, _ y: Int) { self.x = x self.y = y } static prefix func - (this: Self) -> Self { Zomega(-this.x, -this.y) } static func + (lhs: Self, rhs: Self) -> Self { Zomega(lhs.x + rhs.x, lhs.y + rhs.y) } static func - (lhs: Self, rhs: Self) -> Self { Zomega(lhs.x - rhs.x, lhs.y - rhs.y) } static func * (lhs: Self, rhs: Self) -> Self { Zomega(lhs.x * rhs.x - lhs.y * rhs.y, lhs.x * rhs.y + lhs.y * rhs.x - lhs.y * rhs.y) } } var scanner = Scanner() var visited = Set() var a = Zomega(1, 0) var b = Zomega(0, 1) var c = Zomega(-1, -1) var z = Zomega(0, 0) visited.insert(z) for x in scanner.next() { switch x { case "a": z = z + a a = -a (b, c) = (-c, -b) case "b": z = z + b b = -b (c, a) = (-a, -c) default: z = z + c c = -c (a, b) = (-b, -a) } visited.insert(z) } print(visited.count)