結果
問題 | No.762 PDCAパス |
ユーザー |
![]() |
提出日時 | 2019-03-22 10:57:13 |
言語 | Go (1.23.4) |
結果 |
AC
|
実行時間 | 150 ms / 2,000 ms |
コード長 | 1,769 bytes |
コンパイル時間 | 10,832 ms |
コンパイル使用メモリ | 235,680 KB |
実行使用メモリ | 14,592 KB |
最終ジャッジ日時 | 2024-09-19 02:17:56 |
合計ジャッジ時間 | 13,602 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
package mainimport ("bufio""fmt""math""os""strconv")func main() {var n, m ints := ""_, _ = fmt.Scan(&n, &m, &s)r := []rune(s)pdca := []rune("PDCA")mod := int(math.Pow(10, 9)) + 7sr := 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 -> tofor 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 := 0for i := len(pdca) - 1; i >= 0; i-- {for _, j := range sr[i] {if string(r[j]) == "A" {p[j] = 1} else {sum := 0for _, k := range path[j] {sum += p[k]}p[j] = sum % modif i == 0 {ans += p[j]}}}}fmt.Println(ans % mod)}