結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-04-08 23:08:21
言語 Go
(1.22.1)
結果
AC  
実行時間 325 ms / 3,000 ms
コード長 2,892 bytes
コンパイル時間 16,473 ms
コンパイル使用メモリ 239,012 KB
実行使用メモリ 59,492 KB
最終ジャッジ日時 2024-04-14 18:59:48
合計ジャッジ時間 30,087 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 166 ms
14,424 KB
testcase_03 AC 214 ms
19,184 KB
testcase_04 AC 175 ms
16,804 KB
testcase_05 AC 126 ms
12,520 KB
testcase_06 AC 192 ms
18,864 KB
testcase_07 AC 185 ms
11,140 KB
testcase_08 AC 47 ms
7,000 KB
testcase_09 AC 187 ms
14,600 KB
testcase_10 AC 84 ms
9,888 KB
testcase_11 AC 74 ms
8,264 KB
testcase_12 AC 140 ms
12,732 KB
testcase_13 AC 241 ms
19,292 KB
testcase_14 AC 151 ms
13,372 KB
testcase_15 AC 214 ms
16,488 KB
testcase_16 AC 291 ms
21,876 KB
testcase_17 AC 124 ms
9,660 KB
testcase_18 AC 215 ms
14,496 KB
testcase_19 AC 104 ms
12,436 KB
testcase_20 AC 256 ms
18,640 KB
testcase_21 AC 88 ms
11,604 KB
testcase_22 AC 325 ms
23,948 KB
testcase_23 AC 319 ms
23,788 KB
testcase_24 AC 315 ms
22,256 KB
testcase_25 AC 310 ms
24,324 KB
testcase_26 AC 317 ms
25,136 KB
testcase_27 AC 322 ms
24,800 KB
testcase_28 AC 312 ms
21,580 KB
testcase_29 AC 310 ms
22,096 KB
testcase_30 AC 320 ms
22,476 KB
testcase_31 AC 319 ms
22,896 KB
testcase_32 AC 314 ms
22,868 KB
testcase_33 AC 313 ms
22,852 KB
testcase_34 AC 321 ms
21,844 KB
testcase_35 AC 310 ms
21,772 KB
testcase_36 AC 311 ms
22,400 KB
testcase_37 AC 312 ms
22,912 KB
testcase_38 AC 310 ms
21,740 KB
testcase_39 AC 312 ms
23,056 KB
testcase_40 AC 298 ms
21,948 KB
testcase_41 AC 309 ms
22,740 KB
testcase_42 AC 273 ms
17,528 KB
testcase_43 AC 269 ms
19,172 KB
testcase_44 AC 274 ms
20,048 KB
testcase_45 AC 270 ms
20,304 KB
testcase_46 AC 271 ms
19,812 KB
testcase_47 AC 213 ms
14,216 KB
testcase_48 AC 213 ms
13,472 KB
testcase_49 AC 211 ms
14,400 KB
testcase_50 AC 213 ms
14,128 KB
testcase_51 AC 215 ms
14,388 KB
testcase_52 AC 140 ms
51,108 KB
testcase_53 AC 137 ms
59,492 KB
testcase_54 AC 137 ms
59,484 KB
testcase_55 AC 137 ms
8,384 KB
testcase_56 AC 136 ms
8,372 KB
testcase_57 AC 137 ms
8,372 KB
testcase_58 AC 136 ms
34,328 KB
testcase_59 AC 319 ms
22,512 KB
testcase_60 AC 74 ms
10,136 KB
testcase_61 AC 136 ms
8,360 KB
testcase_62 AC 8 ms
6,940 KB
testcase_63 AC 174 ms
9,316 KB
testcase_64 AC 180 ms
10,356 KB
testcase_65 AC 170 ms
9,304 KB
testcase_66 AC 135 ms
8,388 KB
testcase_67 AC 246 ms
16,112 KB
testcase_68 AC 133 ms
8,384 KB
testcase_69 AC 239 ms
16,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
)

// 顺序遍历每个单词,问之前是否见过类似的单词.
//  `类似`: 最多交换一次相邻字符,可以得到相同的单词.
//  n<=2e5 sum(len(s))<=1e6
func ConditionalReflection(words []string) []bool {

	R := NewRollingHash(13331)
	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 RollingHash struct {
	base  uint
	power []uint
}

// 131/13331/1713302033171(回文素数)
func NewRollingHash(base uint) *RollingHash {
	return &RollingHash{
		base:  base,
		power: []uint{1},
	}
}

func (r *RollingHash) Build(s string) (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])
	}
	return hashTable
}

func (r *RollingHash) Query(sTable []uint, start, end int) uint {
	r.expand(end - start)
	return sTable[end] - sTable[start]*r.power[end-start]
}

func (r *RollingHash) Combine(h1, h2 uint, h2len int) uint {
	r.expand(h2len)
	return h1*r.power[h2len] + h2
}

func (r *RollingHash) AddChar(hash uint, c byte) uint {
	return hash*r.base + uint(c)
}

// 两个字符串的最长公共前缀长度.
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
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
0