結果

問題 No.2606 Mirror Relay
ユーザー 草苺奶昔草苺奶昔
提出日時 2024-02-17 21:40:25
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 6,320 bytes
コンパイル時間 15,044 ms
コンパイル使用メモリ 224,916 KB
実行使用メモリ 50,504 KB
最終ジャッジ日時 2024-02-17 21:40:46
合計ジャッジ時間 19,134 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 19 ms
7,936 KB
testcase_40 AC 18 ms
8,064 KB
testcase_41 AC 18 ms
8,064 KB
testcase_42 AC 19 ms
7,936 KB
testcase_43 AC 18 ms
7,936 KB
testcase_44 AC 18 ms
8,064 KB
testcase_45 AC 17 ms
8,064 KB
testcase_46 AC 17 ms
8,064 KB
testcase_47 AC 19 ms
10,128 KB
testcase_48 AC 20 ms
7,936 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 15 ms
10,048 KB
testcase_62 AC 15 ms
10,164 KB
testcase_63 WA -
testcase_64 AC 15 ms
8,060 KB
testcase_65 WA -
testcase_66 AC 15 ms
8,060 KB
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 AC 1 ms
6,676 KB
testcase_71 AC 1 ms
6,676 KB
testcase_72 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	yuki2606()
}

func demo() {
	pt := NewPalindromicTree()
	pt.AddString("eertree")

	fmt.Println(pt.Size() - 2)     // 本质不同回文子串个数(不超过O(n))
	fmt.Println(pt.GetFrequency()) // 每个顶点对应的回文串出现的次数
	for pos := int32(0); pos < pt.Size(); pos++ {
		res := pt.GetPalindrome(pos)
		fmt.Println(string(res))
	}

	s := "abca"
	for i := 0; i < len(s); i++ {
		pos := pt.Add(int32(s[i]))
		node := pt.GetNode(pos)
		fmt.Println(node.Link, node.Indexes, string(pt.GetPalindrome(pos)))
	}
}

// https://yukicoder.me/problems/no/2606
// 给定一个字符串s.
// 向一个空字符x串插入字符,如果x为回文,则获得 x的长度*x在s中出现的次数 的分数.
// 求最终可能的最大分数.
// n<=2e5
func yuki2606() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var s string
	fmt.Fscan(in, &s)

	T := NewPalindromicTree()
	T.AddString(s)
	counter := T.GetFrequency()
	n := T.Size()
	dp := make([]int32, n)
	for i := int32(2); i < n; i++ {
		node := T.GetNode(i)
		count := counter[i]
		length := node.Length
		fail := node.Link
		dp[i] = maxInt32(dp[i], dp[fail]+count*length)
	}

	fmt.Fprintln(out, maxsInt32(dp))
}

type Node struct {
	Next      map[int32]int32 // 子节点.
	Link      int32           // suffix link,指向当前回文串的最长真回文后缀的位置
	Length    int32           // 结点代表的回文串的长度
	Indexes   []int32         // 哪些最长真回文后缀为当前节点对应的回文
	deltaLink int32
}

type PalindromicTree struct {
	Ords    []int32
	Nodes   []*Node
	lastPos int32 // 当前的最长回文后缀
}

func NewPalindromicTree() *PalindromicTree {
	res := &PalindromicTree{}
	res.Nodes = append(res.Nodes, res.newNode(0, -1)) // 奇根,长为 -1
	res.Nodes = append(res.Nodes, res.newNode(0, 0))  // 偶根,长为 0
	return res
}

// !添加一个字符,返回以这个字符为后缀的最长回文串的位置pos.
func (pt *PalindromicTree) Add(x int32) int32 {
	pos := int32(len(pt.Ords))
	pt.Ords = append(pt.Ords, x)
	cur := pt.findPrevPalindrome(pt.lastPos)
	_, hasKey := pt.Nodes[cur].Next[x]
	if !hasKey {
		pt.Nodes[cur].Next[x] = int32(len(pt.Nodes))
	}
	pt.lastPos = pt.Nodes[cur].Next[x]
	if !hasKey {
		pt.Nodes = append(pt.Nodes, pt.newNode(-1, pt.Nodes[cur].Length+2))
		if pt.Nodes[len(pt.Nodes)-1].Length == 1 {
			pt.Nodes[len(pt.Nodes)-1].Link = 1
		} else {
			pt.Nodes[len(pt.Nodes)-1].Link = pt.Nodes[pt.findPrevPalindrome(pt.Nodes[cur].Link)].Next[x]
		}

		if pt.diff(pt.lastPos) == pt.diff(pt.Nodes[len(pt.Nodes)-1].Link) {
			pt.Nodes[len(pt.Nodes)-1].deltaLink = pt.Nodes[pt.Nodes[len(pt.Nodes)-1].Link].deltaLink
		} else {
			pt.Nodes[len(pt.Nodes)-1].deltaLink = pt.Nodes[len(pt.Nodes)-1].Link
		}
	}

	pt.Nodes[pt.lastPos].Indexes = append(pt.Nodes[pt.lastPos].Indexes, pos)
	return pt.lastPos
}

func (pt *PalindromicTree) AddString(s string) {
	if len(s) == 0 {
		return
	}
	for _, v := range s {
		pt.Add(v)
	}
}

// 在每次调用AddChar(x)之后使用,更新dp.
//   - init(pos, start): 初始化顶点pos的dp值,对应回文串s[start:].
//   - apply(pos, prePos): 用prePos(fail指针指向的位置)更新pos.
//     返回值: 本次更新的回文串的顶点.
func (pt *PalindromicTree) UpdateDp(init func(pos, start int32), apply func(pos, pre int32)) (updated []int32) {
	i := int32(len(pt.Ords) - 1)
	id := pt.lastPos
	for pt.Nodes[id].Length > 0 {
		init(id, i+1-pt.Nodes[pt.Nodes[id].deltaLink].Length-pt.diff(id))
		if pt.Nodes[id].deltaLink != pt.Nodes[id].Link {
			apply(id, pt.Nodes[id].Link)
		}
		updated = append(updated, id)
		id = pt.Nodes[id].deltaLink
	}
	return
}

// 求出每个顶点代表的回文串出现的次数.
func (pt *PalindromicTree) GetFrequency() []int32 {
	res := make([]int32, pt.Size())
	for i := pt.Size() - 1; i >= 1; i-- { // 除去根节点(奇根)
		res[i] += int32(len(pt.Nodes[i].Indexes))
		res[pt.Nodes[i].Link] += res[i] // 长回文包含短回文
	}
	return res
}

// 当前字符的本质不同回文串个数.
func (pt *PalindromicTree) CountPalindromes() int32 {
	res := int32(0)
	for i := int32(1); i < pt.Size(); i++ { // 除去根节点(奇根)
		res += int32(len(pt.Nodes[i].Indexes))
	}
	return res
}

// 输出每个顶点代表的回文串.
func (pt *PalindromicTree) GetPalindrome(pos int32) []int32 {
	if pos == 0 {
		return []int32{-1}
	}
	if pos == 1 {
		return []int32{0}
	}
	var res []int32
	// 在偶树/奇树中找到当前节点的回文串
	pt.outputDfs(0, pos, &res)
	pt.outputDfs(1, pos, &res)
	start := len(res) - 1
	if pt.Nodes[pos].Length&1 == 1 {
		start--
	}
	for i := start; i >= 0; i-- {
		res = append(res, res[i])
	}
	return res
}

// 回文树中的顶点个数.(包含两个奇偶虚拟顶点)
func (pt *PalindromicTree) Size() int32 {
	return int32(len(pt.Nodes))
}

// 返回pos位置的回文串顶点.
func (pt *PalindromicTree) GetNode(pos int32) *Node {
	return pt.Nodes[pos]
}

func (pt *PalindromicTree) newNode(link, length int32) *Node {
	return &Node{
		Next:      make(map[int32]int32),
		Link:      link,
		Length:    length,
		deltaLink: -1,
	}
}

// 沿着失配指针找到第一个满足 x+s+x 是原串回文后缀的位置.
func (pt *PalindromicTree) findPrevPalindrome(cur int32) int32 {
	pos := int32(len(pt.Ords) - 1)
	for {
		rev := pos - 1 - pt.Nodes[cur].Length
		// !插入当前字符的条件str[i]==str[i-len-1]
		if rev >= 0 && pt.Ords[rev] == pt.Ords[len(pt.Ords)-1] {
			break
		}
		cur = pt.Nodes[cur].Link
	}
	return cur
}

// 当前位置的回文串长度减去当前回文串的最长后缀回文串的长度.
func (pt *PalindromicTree) diff(pos int32) int32 {
	if pt.Nodes[pos].Link <= 0 {
		return -1
	}
	return pt.Nodes[pos].Length - pt.Nodes[pt.Nodes[pos].Link].Length
}

func (pt *PalindromicTree) outputDfs(cur, id int32, res *[]int32) bool {
	if cur == id {
		return true
	}
	for key, next := range pt.Nodes[cur].Next {
		if pt.outputDfs(next, id, res) {
			*res = append(*res, key)
			return true
		}
	}
	return false
}

func maxInt32(a, b int32) int32 {
	if a > b {
		return a
	}
	return b
}

func maxsInt32(nums []int32) int32 {
	res := nums[0]
	for _, v := range nums {
		if v > res {
			res = v
		}
	}
	return res
}
0