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 }