結果

問題 No.762 PDCAパス
ユーザー tsuchinagatsuchinaga
提出日時 2019-03-22 10:57:13
言語 Go
(1.22.1)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 1,769 bytes
コンパイル時間 12,301 ms
コンパイル使用メモリ 209,700 KB
実行使用メモリ 14,372 KB
最終ジャッジ日時 2023-10-19 05:58:47
合計ジャッジ時間 15,430 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 1 ms
4,368 KB
testcase_10 AC 1 ms
4,368 KB
testcase_11 AC 1 ms
4,368 KB
testcase_12 AC 1 ms
4,368 KB
testcase_13 AC 1 ms
4,368 KB
testcase_14 AC 2 ms
4,368 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 1 ms
4,368 KB
testcase_17 AC 2 ms
4,368 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 15 ms
4,368 KB
testcase_23 AC 15 ms
4,368 KB
testcase_24 AC 180 ms
14,372 KB
testcase_25 AC 179 ms
14,368 KB
testcase_26 AC 17 ms
5,616 KB
testcase_27 AC 17 ms
5,616 KB
testcase_28 AC 157 ms
12,056 KB
testcase_29 AC 157 ms
12,056 KB
testcase_30 AC 122 ms
9,216 KB
testcase_31 AC 124 ms
9,224 KB
testcase_32 AC 124 ms
9,220 KB
testcase_33 AC 89 ms
6,760 KB
testcase_34 AC 91 ms
8,020 KB
testcase_35 AC 91 ms
8,016 KB
testcase_36 AC 44 ms
5,040 KB
testcase_37 AC 44 ms
5,044 KB
testcase_38 AC 44 ms
5,040 KB
testcase_39 AC 43 ms
5,044 KB
testcase_40 AC 42 ms
5,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func main() {
	var n, m int
	s := ""
	_, _ = fmt.Scan(&n, &m, &s)
	r := []rune(s)
	pdca := []rune("PDCA")
	mod := int(math.Pow(10, 9)) + 7

	sr := make(map[int][]int, 0) // PDCAの位置
	for i, c := range r {
		if c == pdca[0] {
			sr[0] = append(sr[0], i)
		} else if c == pdca[1] {
			sr[1] = append(sr[1], i)
		} else if c == pdca[2] {
			sr[2] = append(sr[2], i)
		} else {
			sr[3] = append(sr[3], i)
		}
	}

	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// P -> D, D -> C, C -> Aのパスのみを取得する
	path := make(map[int][]int, 0) // from -> to
	for i := 0; i < m; i++ {
		sc.Scan()
		n1, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		n2, _ := strconv.Atoi(sc.Text())

		// fmt.Println(n1, string(r[n1-1]), n2, string(r[n2-1]))

		if (r[n1-1] == pdca[0] && r[n2-1] == pdca[1]) ||
			(r[n1-1] == pdca[1] && r[n2-1] == pdca[2]) ||
			(r[n1-1] == pdca[2] && r[n2-1] == pdca[3]) {
			path[n1-1] = append(path[n1-1], n2-1)
		} else if (r[n2-1] == pdca[0] && r[n1-1] == pdca[1]) ||
			(r[n2-1] == pdca[1] && r[n1-1] == pdca[2]) ||
			(r[n2-1] == pdca[2] && r[n1-1] == pdca[3]) {
			path[n2-1] = append(path[n2-1], n1-1)
		}
	}

	// fmt.Println(path)

	// A, D, C, Pの順に、移動先が何通りになるかの評価
	// Aの移動先はないので、常に1通りになる
	// この時10^9 + 7を超える場合は余りを保持するようにする
	p := make([]int, n)
	ans := 0
	for i := len(pdca) - 1; i >= 0; i-- {
		for _, j := range sr[i] {
			if string(r[j]) == "A" {
				p[j] = 1
			} else {
				sum := 0
				for _, k := range path[j] {
					sum += p[k]
				}
				p[j] = sum % mod

				if i == 0 {
					ans += p[j]
				}
			}
		}
	}

	fmt.Println(ans % mod)
}
0