結果

問題 No.1171 Runs in Subsequences
ユーザー semisagisemisagi
提出日時 2020-08-14 21:50:19
言語 Swift
(5.10.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 3,562 bytes
コンパイル時間 1,418 ms
コンパイル使用メモリ 133,056 KB
実行使用メモリ 92,420 KB
最終ジャッジ日時 2024-10-10 15:01:15
合計ジャッジ時間 4,084 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
9,216 KB
testcase_01 AC 9 ms
9,344 KB
testcase_02 AC 8 ms
9,216 KB
testcase_03 AC 9 ms
9,216 KB
testcase_04 AC 8 ms
9,344 KB
testcase_05 AC 9 ms
9,344 KB
testcase_06 AC 8 ms
9,216 KB
testcase_07 AC 9 ms
9,344 KB
testcase_08 AC 8 ms
9,216 KB
testcase_09 AC 9 ms
9,344 KB
testcase_10 AC 10 ms
9,600 KB
testcase_11 AC 10 ms
9,728 KB
testcase_12 AC 10 ms
9,600 KB
testcase_13 AC 9 ms
9,472 KB
testcase_14 AC 10 ms
9,600 KB
testcase_15 AC 255 ms
89,348 KB
testcase_16 AC 250 ms
88,192 KB
testcase_17 AC 264 ms
92,248 KB
testcase_18 AC 264 ms
92,264 KB
testcase_19 AC 178 ms
92,204 KB
testcase_20 AC 178 ms
92,420 KB
testcase_21 AC 187 ms
92,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 {
        let value = peek()
        index += 1
        return value
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

let MOD = 1000000007

struct Zn: CustomStringConvertible {
    let n: Int

    init(_ n: Int) {
        self.n = n
    }

    var description: String {
        String(self.n)
    }

    static var zero = Zn(0)
    static var one = Zn(1)

    static func + (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n + rhs.n
        return Zn(n < MOD ? n : n - MOD)
    }

    static func + (lhs: Self, rhs: Int) -> Self {
        return lhs + Zn(rhs)
    }

    static func + (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) + rhs
    }

    static func - (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n - rhs.n
        return Zn(n >= 0 ? n : n + MOD)
    }

    static func - (lhs: Self, rhs: Int) -> Self {
        return lhs - Zn(rhs)
    }

    static func - (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) - rhs
    }

    static func * (lhs: Self, rhs: Self) -> Self {
        Zn((lhs.n * rhs.n) % MOD)
    }

    static func * (lhs: Self, rhs: Int) -> Self {
        return lhs * Zn(rhs)
    }

    static func * (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) * rhs
    }

    static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }

    static func += (lhs: inout Self, rhs: Int) {
        lhs = lhs + Zn(rhs)
    }

    static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }

    static func -= (lhs: inout Self, rhs: Int) {
        lhs = lhs - Zn(rhs)
    }

    static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }

    static func *= (lhs: inout Self, rhs: Int) {
        lhs = lhs * Zn(rhs)
    }

    func pow(_ n: Int) -> Self {
        if n == 0 {
            return Zn(1)
        } else if n % 2 == 1 {
            return self * self.pow(n - 1)
        } else {
            return (self * self).pow(n / 2)
        }
    }

    var inverse: Self {
        self.pow(MOD - 2)
    }
}

struct Array2D<Element> {
    let n1: Int
    let n2: Int
    private var elements: [Element]

    init(repeating: Element, _ n1: Int, _ n2: Int) {
        self.n1 = n1
        self.n2 = n2
        self.elements = [Element](repeating: repeating, count: n1 * n2)
    }

    subscript(i: Int, j: Int) -> Element {
        get {
            elements[i * n2 + j]
        }
        set {
            elements[i * n2 + j] = newValue
        }
    }
}

var scanner = Scanner()

let S = scanner.next().map({ Int($0.asciiValue!) - Int(Character("a").asciiValue!) })
let N = S.count

var count = Array2D(repeating: Zn.zero, N + 1, 26)
var dp = Array2D(repeating: Zn.zero, N + 1, 26)

for i in S.indices {
    count[i + 1, S[i]] += 1
    dp[i + 1, S[i]] += 1
    for j in 0 ..< 26 {
        count[i + 1, j] += count[i, j]
        count[i + 1, S[i]] += count[i, j]
        dp[i + 1, j] += dp[i, j]
        if S[i] == j {
            dp[i + 1, j] += dp[i, j]
        } else {
            dp[i + 1, j] += dp[i, j] + count[i, j]
        }
    }
}

let answer = (0 ..< 26).map({ dp[N, $0] }).reduce(.zero, +)
print(answer)
0