結果
問題 | No.2102 [Cherry Alpha *] Conditional Reflection |
ユーザー | 草苺奶昔 |
提出日時 | 2023-12-16 16:39:13 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,253 bytes |
コンパイル時間 | 15,470 ms |
コンパイル使用メモリ | 221,416 KB |
実行使用メモリ | 59,488 KB |
最終ジャッジ日時 | 2024-09-27 07:40:44 |
合計ジャッジ時間 | 37,515 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 152 ms
12,416 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 52 ms
6,656 KB |
testcase_09 | WA | - |
testcase_10 | AC | 101 ms
10,112 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 100 ms
12,160 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | AC | 370 ms
19,840 KB |
testcase_43 | AC | 367 ms
20,424 KB |
testcase_44 | AC | 366 ms
17,456 KB |
testcase_45 | AC | 369 ms
19,456 KB |
testcase_46 | AC | 365 ms
20,820 KB |
testcase_47 | WA | - |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | WA | - |
testcase_52 | AC | 252 ms
58,880 KB |
testcase_53 | AC | 253 ms
58,880 KB |
testcase_54 | AC | 245 ms
59,488 KB |
testcase_55 | AC | 238 ms
6,912 KB |
testcase_56 | AC | 239 ms
6,908 KB |
testcase_57 | AC | 242 ms
6,784 KB |
testcase_58 | AC | 243 ms
33,640 KB |
testcase_59 | AC | 409 ms
22,656 KB |
testcase_60 | AC | 92 ms
8,960 KB |
testcase_61 | WA | - |
testcase_62 | AC | 12 ms
5,376 KB |
testcase_63 | AC | 277 ms
10,404 KB |
testcase_64 | AC | 273 ms
9,108 KB |
testcase_65 | AC | 275 ms
9,080 KB |
testcase_66 | AC | 237 ms
6,784 KB |
testcase_67 | AC | 343 ms
15,348 KB |
testcase_68 | AC | 242 ms
6,784 KB |
testcase_69 | AC | 324 ms
14,848 KB |
ソースコード
package main import ( "bufio" "fmt" "os" ) // 顺序遍历每个单词,问之前是否见过类似的单词. // // `类似`: 最多交换一次相邻字符,可以得到相同的单词. // n<=2e5 sum(len(s))<=1e6 func ConditionalReflection(words []string) []bool { R := NewRollingHash(13331, 1000000007) res := make([]bool, len(words)) visited := make(map[int64]struct{}) for i := 0; i < len(words); i++ { m := len(words[i]) word := words[i] table := R.Build(word) hashes := []int64{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 int64, mod int64) int64 { if len(s) == 0 { return 0 } res := int64(0) for i := 0; i < len(s); i++ { res = (res*base + int64(s[i])) % mod } return res } type RollingHash struct { base int64 mod int64 power []int64 } // 131/13331/1713302033171(回文素数) func NewRollingHash(base int64, mod int64) *RollingHash { return &RollingHash{ base: base, mod: mod, power: []int64{1}, } } func (r *RollingHash) Build(s S) (hashTable []int64) { sz := len(s) hashTable = make([]int64, sz+1) for i := 0; i < sz; i++ { hashTable[i+1] = (hashTable[i]*r.base + int64(s[i])) % r.mod } return hashTable } func (r *RollingHash) Query(sTable []int64, start, end int) int64 { r.expand(end - start) res := (sTable[end] - sTable[start]*r.power[end-start]%r.mod) if res < 0 { res += r.mod } return res } func (r *RollingHash) Combine(h1, h2 int64, h2len int) int64 { r.expand(h2len) return (h1*r.power[h2len] + h2) % r.mod } func (r *RollingHash) AddChar(hash int64, c byte) int64 { return (hash*r.base + int64(c)) % r.mod } // 两个字符串的最长公共前缀长度. func (r *RollingHash) LCP(sTable []int64, start1, end1 int, tTable []int64, 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([]int64, 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 }