結果
問題 | No.2102 [Cherry Alpha *] Conditional Reflection |
ユーザー | 草苺奶昔 |
提出日時 | 2023-12-16 17:30:45 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 416 ms / 3,000 ms |
コード長 | 3,198 bytes |
コンパイル時間 | 11,401 ms |
コンパイル使用メモリ | 239,388 KB |
実行使用メモリ | 59,008 KB |
最終ジャッジ日時 | 2024-09-27 07:46:59 |
合計ジャッジ時間 | 35,549 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 213 ms
13,568 KB |
testcase_03 | AC | 273 ms
18,672 KB |
testcase_04 | AC | 220 ms
14,720 KB |
testcase_05 | AC | 160 ms
12,288 KB |
testcase_06 | AC | 234 ms
15,232 KB |
testcase_07 | AC | 290 ms
11,008 KB |
testcase_08 | AC | 55 ms
6,656 KB |
testcase_09 | AC | 259 ms
14,628 KB |
testcase_10 | AC | 102 ms
9,688 KB |
testcase_11 | AC | 99 ms
7,936 KB |
testcase_12 | AC | 184 ms
12,544 KB |
testcase_13 | AC | 318 ms
17,848 KB |
testcase_14 | AC | 186 ms
14,452 KB |
testcase_15 | AC | 269 ms
16,360 KB |
testcase_16 | AC | 371 ms
20,448 KB |
testcase_17 | AC | 188 ms
9,720 KB |
testcase_18 | AC | 307 ms
14,612 KB |
testcase_19 | AC | 130 ms
12,308 KB |
testcase_20 | AC | 334 ms
19,396 KB |
testcase_21 | AC | 100 ms
11,480 KB |
testcase_22 | AC | 393 ms
20,864 KB |
testcase_23 | AC | 402 ms
20,992 KB |
testcase_24 | AC | 409 ms
20,992 KB |
testcase_25 | AC | 395 ms
22,248 KB |
testcase_26 | AC | 393 ms
20,864 KB |
testcase_27 | AC | 413 ms
22,268 KB |
testcase_28 | AC | 394 ms
20,992 KB |
testcase_29 | AC | 414 ms
21,808 KB |
testcase_30 | AC | 394 ms
20,848 KB |
testcase_31 | AC | 413 ms
20,864 KB |
testcase_32 | AC | 400 ms
22,312 KB |
testcase_33 | AC | 415 ms
20,864 KB |
testcase_34 | AC | 409 ms
21,120 KB |
testcase_35 | AC | 404 ms
22,168 KB |
testcase_36 | AC | 408 ms
21,092 KB |
testcase_37 | AC | 416 ms
22,016 KB |
testcase_38 | AC | 403 ms
20,864 KB |
testcase_39 | AC | 403 ms
22,144 KB |
testcase_40 | AC | 399 ms
22,952 KB |
testcase_41 | AC | 394 ms
21,616 KB |
testcase_42 | AC | 374 ms
16,896 KB |
testcase_43 | AC | 369 ms
17,376 KB |
testcase_44 | AC | 371 ms
19,956 KB |
testcase_45 | AC | 366 ms
20,128 KB |
testcase_46 | AC | 369 ms
20,024 KB |
testcase_47 | AC | 317 ms
13,600 KB |
testcase_48 | AC | 320 ms
13,712 KB |
testcase_49 | AC | 315 ms
13,376 KB |
testcase_50 | AC | 317 ms
13,548 KB |
testcase_51 | AC | 316 ms
13,920 KB |
testcase_52 | AC | 254 ms
59,008 KB |
testcase_53 | AC | 254 ms
59,008 KB |
testcase_54 | AC | 256 ms
59,008 KB |
testcase_55 | AC | 251 ms
6,784 KB |
testcase_56 | AC | 244 ms
6,784 KB |
testcase_57 | AC | 242 ms
6,912 KB |
testcase_58 | AC | 248 ms
33,736 KB |
testcase_59 | AC | 408 ms
22,656 KB |
testcase_60 | AC | 93 ms
8,448 KB |
testcase_61 | AC | 242 ms
6,856 KB |
testcase_62 | AC | 13 ms
5,376 KB |
testcase_63 | AC | 276 ms
14,336 KB |
testcase_64 | AC | 274 ms
9,088 KB |
testcase_65 | AC | 276 ms
16,468 KB |
testcase_66 | AC | 245 ms
6,784 KB |
testcase_67 | AC | 342 ms
15,460 KB |
testcase_68 | AC | 242 ms
6,772 KB |
testcase_69 | AC | 323 ms
15,448 KB |
ソースコード
package main import ( "bufio" "fmt" "os" ) // 顺序遍历每个单词,问之前是否见过类似的单词. // // `类似`: 最多交换一次相邻字符,可以得到相同的单词. // n<=2e5 sum(len(s))<=1e6 func ConditionalReflection(words []string) []bool { R := NewRollingHash(37, 2102001800968) res := make([]bool, len(words)) visited := make(map[uint]struct{}) for i := 0; i < len(words); i++ { m := len(words[i]) word := words[i] table := R.Build(word) hashes := []uint{R.Query(table, 0, m)} // 不交换 for j := 0; j < m-1; j++ { // 交换 newWord := string(word[j+1]) + string(word[j]) mid := R.Query(R.Build(newWord), 0, 2) left := R.Query(table, 0, j) right := R.Query(table, j+2, m) left = R.Combine(left, mid, 2) left = R.Combine(left, right, m-j-2) hashes = append(hashes, left) } for _, h := range hashes { if _, ok := visited[h]; ok { res[i] = true break } } visited[hashes[0]] = struct{}{} } return res } func main() { // https://yukicoder.me/problems/no/2102 in := bufio.NewReader(os.Stdin) out := bufio.NewWriter(os.Stdout) defer out.Flush() var n int fmt.Fscan(in, &n) words := make([]string, n) for i := 0; i < n; i++ { fmt.Fscan(in, &words[i]) } res := ConditionalReflection(words) for i := 0; i < n; i++ { if res[i] { fmt.Fprintln(out, "Yes") } else { fmt.Fprintln(out, "No") } } } type S = string func GetHash(s S, base uint, mod uint) uint { if len(s) == 0 { return 0 } res := uint(0) for i := 0; i < len(s); i++ { res = (res*base + uint(s[i])) % mod } return res } type RollingHash struct { base uint mod uint power []uint } // 131/13331/1713302033171(回文素数) func NewRollingHash(base uint, mod uint) *RollingHash { return &RollingHash{ base: base, mod: mod, power: []uint{1}, } } func (r *RollingHash) Build(s S) (hashTable []uint) { sz := len(s) hashTable = make([]uint, sz+1) for i := 0; i < sz; i++ { hashTable[i+1] = (hashTable[i]*r.base + uint(s[i])) % r.mod } return hashTable } func (r *RollingHash) Query(sTable []uint, start, end int) uint { r.expand(end - start) return (r.mod + sTable[end] - sTable[start]*r.power[end-start]%r.mod) % r.mod } func (r *RollingHash) Combine(h1, h2 uint, h2len int) uint { r.expand(h2len) return (h1*r.power[h2len] + h2) % r.mod } func (r *RollingHash) AddChar(hash uint, c byte) uint { return (hash*r.base + uint(c)) % r.mod } // 两个字符串的最长公共前缀长度. func (r *RollingHash) LCP(sTable []uint, start1, end1 int, tTable []uint, start2, end2 int) int { len1 := end1 - start1 len2 := end2 - start2 len := min(len1, len2) low := 0 high := len + 1 for high-low > 1 { mid := (low + high) / 2 if r.Query(sTable, start1, start1+mid) == r.Query(tTable, start2, start2+mid) { low = mid } else { high = mid } } return low } func (r *RollingHash) expand(sz int) { if len(r.power) < sz+1 { preSz := len(r.power) r.power = append(r.power, make([]uint, sz+1-preSz)...) for i := preSz - 1; i < sz; i++ { r.power[i+1] = (r.power[i] * r.base) % r.mod } } } func min(a, b int) int { if a < b { return a } return b }