結果
問題 | No.2606 Mirror Relay |
ユーザー | 草苺奶昔 |
提出日時 | 2024-02-17 21:43:09 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,295 bytes |
コンパイル時間 | 14,662 ms |
コンパイル使用メモリ | 227,212 KB |
実行使用メモリ | 51,648 KB |
最終ジャッジ日時 | 2024-09-28 23:56:19 |
合計ジャッジ時間 | 14,366 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,820 KB |
testcase_07 | AC | 2 ms
6,820 KB |
testcase_08 | AC | 1 ms
6,816 KB |
testcase_09 | AC | 1 ms
6,820 KB |
testcase_10 | AC | 1 ms
6,816 KB |
testcase_11 | AC | 1 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,820 KB |
testcase_13 | AC | 2 ms
6,820 KB |
testcase_14 | AC | 1 ms
6,816 KB |
testcase_15 | AC | 1 ms
6,816 KB |
testcase_16 | AC | 1 ms
6,820 KB |
testcase_17 | AC | 1 ms
6,820 KB |
testcase_18 | AC | 1 ms
6,820 KB |
testcase_19 | AC | 2 ms
6,816 KB |
testcase_20 | AC | 3 ms
6,816 KB |
testcase_21 | AC | 2 ms
6,820 KB |
testcase_22 | AC | 2 ms
6,816 KB |
testcase_23 | AC | 2 ms
6,820 KB |
testcase_24 | AC | 2 ms
6,824 KB |
testcase_25 | AC | 2 ms
6,820 KB |
testcase_26 | AC | 2 ms
6,816 KB |
testcase_27 | AC | 2 ms
6,820 KB |
testcase_28 | AC | 1 ms
6,816 KB |
testcase_29 | AC | 2 ms
6,816 KB |
testcase_30 | AC | 1 ms
6,816 KB |
testcase_31 | AC | 2 ms
6,820 KB |
testcase_32 | AC | 2 ms
6,820 KB |
testcase_33 | AC | 2 ms
6,816 KB |
testcase_34 | AC | 1 ms
6,816 KB |
testcase_35 | AC | 1 ms
6,816 KB |
testcase_36 | AC | 2 ms
6,816 KB |
testcase_37 | AC | 2 ms
6,816 KB |
testcase_38 | AC | 2 ms
6,816 KB |
testcase_39 | AC | 22 ms
8,000 KB |
testcase_40 | AC | 20 ms
8,000 KB |
testcase_41 | AC | 21 ms
8,004 KB |
testcase_42 | AC | 21 ms
8,004 KB |
testcase_43 | AC | 21 ms
8,004 KB |
testcase_44 | AC | 21 ms
8,000 KB |
testcase_45 | AC | 22 ms
8,000 KB |
testcase_46 | AC | 22 ms
10,152 KB |
testcase_47 | AC | 22 ms
8,004 KB |
testcase_48 | AC | 20 ms
8,000 KB |
testcase_49 | AC | 47 ms
18,644 KB |
testcase_50 | AC | 42 ms
18,768 KB |
testcase_51 | AC | 48 ms
20,636 KB |
testcase_52 | AC | 40 ms
18,524 KB |
testcase_53 | AC | 51 ms
20,716 KB |
testcase_54 | AC | 48 ms
18,812 KB |
testcase_55 | AC | 73 ms
28,512 KB |
testcase_56 | AC | 45 ms
18,968 KB |
testcase_57 | AC | 43 ms
18,544 KB |
testcase_58 | AC | 53 ms
20,912 KB |
testcase_59 | AC | 19 ms
10,152 KB |
testcase_60 | AC | 17 ms
8,008 KB |
testcase_61 | AC | 18 ms
8,004 KB |
testcase_62 | AC | 18 ms
8,132 KB |
testcase_63 | AC | 19 ms
10,064 KB |
testcase_64 | AC | 19 ms
10,140 KB |
testcase_65 | AC | 21 ms
9,992 KB |
testcase_66 | AC | 18 ms
10,152 KB |
testcase_67 | AC | 18 ms
10,056 KB |
testcase_68 | AC | 19 ms
10,052 KB |
testcase_69 | WA | - |
testcase_70 | AC | 1 ms
6,816 KB |
testcase_71 | AC | 2 ms
6,816 KB |
testcase_72 | AC | 1 ms
6,816 KB |
ソースコード
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([]int, n) for i := int32(2); i < n; i++ { node := T.GetNode(i) count := counter[i] length := node.Length fail := node.Link dp[i] = max(dp[i], dp[fail]+int(count*length)) } fmt.Fprintln(out, maxs(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 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 }