package main import ( "bufio" "fmt" "os" ) func main() { yuki263() // yuki2606() // P3649() // P5496() } // No.263 Common Palindromes Extra // 求两个字符串的公共回文子串的个数 n<=5e5 // https://yukicoder.me/problems/no/263 func yuki263() { in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var s, t string fmt.Fscan(in, &s, &t) T := NewPalindromicTree() // s + "><" + t T.AddString(s) T.Add(65 + 26) T.Add(65 + 27) T.AddString(t) dps := make([]int, T.Size()) dpt := make([]int, T.Size()) lenS := int32(len(s)) for i := 0; i < T.Size(); i++ { for _, j := range T.Nodes[i].Indexes { // 回文出现位置 if j < lenS { dps[i]++ } else if j >= lenS+2 { dpt[i]++ } } } res := 0 for i := T.Size() - 1; i >= 2; i-- { // 按照拓扑序遍历本质不同回文 res += dps[i] * dpt[i] dps[T.Nodes[i].Link] += dps[i] dpt[T.Nodes[i].Link] += dpt[i] } fmt.Fprintln(out, res) } const SIGMA byte = 26 + 2 const OFFSET byte = 65 type Node struct { Next [SIGMA]int32 // 每个字符的转移 Link int32 // suffix link,指向当前回文串的最长真回文后缀的位置 Length int32 // 结点代表的回文串的长度 Indexes []int32 // 哪些位置的最长回文后缀 deltaLink int32 } type PalindromicTreeArray struct { Bytes []byte Nodes []*Node lastPos int32 // 当前字符串(原串前缀)的最长回文后缀 } func NewPalindromicTree() *PalindromicTreeArray { res := &PalindromicTreeArray{} res.Nodes = append(res.Nodes, res.newNode(0, -1)) // 奇根,长为 -1 res.Nodes = append(res.Nodes, res.newNode(0, 0)) // 偶根,长为 0 return res } // !添加一个字符,返回以这个字符为后缀的最长回文串的位置pos. // 每次增加一个字符,本质不同的回文子串个数最多增加 1 个. func (pt *PalindromicTreeArray) Add(x byte) int { x -= OFFSET pos := int32(len(pt.Bytes)) pt.Bytes = append(pt.Bytes, x) cur := pt.findPrevPalindrome(pt.lastPos) hasKey := pt.Nodes[cur].Next[x] != -1 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 int(pt.lastPos) } func (pt *PalindromicTreeArray) AddString(s string) { if len(s) == 0 { return } for i := 0; i < len(s); i++ { pt.Add(s[i]) } } // Palindrome Series 优化DP // https://zhuanlan.zhihu.com/p/92874690 // 在每次调用Add(x)之后使用,更新dp. // - init(pos, start): 初始化顶点pos的dp值,对应回文串s[start:]. // - apply(pos, prePos): 用prePos(fail指针指向的位置)更新pos. // 返回值: 本次更新的回文串的顶点. func (pt *PalindromicTreeArray) UpdateDp(init func(pos, start int), apply func(pos, pre int)) (updated []int) { i := int32(len(pt.Bytes) - 1) id := pt.lastPos for pt.Nodes[id].Length > 0 { init(int(id), int(i+1-pt.Nodes[pt.Nodes[id].deltaLink].Length-pt.diff(id))) if pt.Nodes[id].deltaLink != pt.Nodes[id].Link { apply(int(id), int(pt.Nodes[id].Link)) } updated = append(updated, int(id)) id = pt.Nodes[id].deltaLink } return } // 按照拓扑序进行转移. // from: 后缀连接, to: 当前节点 func (pt *PalindromicTreeArray) Dp(f func(from, to int)) { for i := pt.Size() - 1; i >= 2; i-- { f(int(pt.Nodes[i].Link), i) } } // 求出每个顶点对应的回文串出现的次数. func (pt *PalindromicTreeArray) GetFrequency() []int { res := make([]int, pt.Size()) // !节点编号从大到小,就是 fail 树的拓扑序 for i := pt.Size() - 1; i >= 1; i-- { // 除去根节点(奇根) res[i] += len(pt.Nodes[i].Indexes) res[pt.Nodes[i].Link] += res[i] // 长回文包含短回文 } return res } // 当前字符的本质不同回文串个数. func (pt *PalindromicTreeArray) CountPalindromes() int { res := 0 for i := 1; i < pt.Size(); i++ { // 除去根节点(奇根) res += len(pt.Nodes[i].Indexes) } return res } // 输出每个顶点代表的回文串. func (pt *PalindromicTreeArray) GetPalindrome(pos int) []int { if pos == 0 { return []int{-1} } if pos == 1 { return []int{0} } var res []int // 在偶树/奇树中找到当前节点的回文串 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 } // 回文树中的顶点个数.(包含两个奇偶虚拟顶点) // 一个串的本质不同回文子串个数等于 Size()-2. func (pt *PalindromicTreeArray) Size() int { return len(pt.Nodes) } // 返回pos位置的回文串顶点. func (pt *PalindromicTreeArray) GetNode(pos int) *Node { return pt.Nodes[pos] } func (pt *PalindromicTreeArray) newNode(link, length int32) *Node { res := &Node{ Next: [SIGMA]int32{}, Link: link, Length: length, deltaLink: -1, } for i := range res.Next { res.Next[i] = -1 } return res } // 沿着失配指针找到第一个满足 x+s+x 是原串回文后缀的位置. func (pt *PalindromicTreeArray) findPrevPalindrome(cur int32) int32 { pos := int32(len(pt.Bytes) - 1) for { rev := pos - 1 - pt.Nodes[cur].Length // !插入当前字符的条件str[i]==str[i-len-1] if rev >= 0 && pt.Bytes[rev] == pt.Bytes[len(pt.Bytes)-1] { break } cur = pt.Nodes[cur].Link } return cur } // 当前位置的回文串长度减去当前回文串的最长后缀回文串的长度. func (pt *PalindromicTreeArray) 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 *PalindromicTreeArray) outputDfs(cur, id int, res *[]int) bool { if cur == id { return true } for key, next := range pt.Nodes[cur].Next { if pt.outputDfs(int(next), id, res) { *res = append(*res, int(key)) return true } } return false } func max(a, b int) int { if a > b { return a } return b } func maxs(nums []int) int { res := nums[0] for _, v := range nums { if v > res { res = v } } return res }