結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-04-08 23:07:13
言語 Go
(1.22.1)
結果
RE  
実行時間 -
コード長 3,314 bytes
コンパイル時間 14,613 ms
コンパイル使用メモリ 231,252 KB
実行使用メモリ 59,488 KB
最終ジャッジ日時 2024-04-14 18:58:35
合計ジャッジ時間 23,489 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 AC 139 ms
51,220 KB
testcase_53 AC 137 ms
51,220 KB
testcase_54 AC 136 ms
59,488 KB
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 WA -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// API:
//  RollingHash(base) : 以base为底的哈希函数.
//  Build(s) : O(n)返回s的哈希值表.
//  Query(sTable, l, r) : 返回s[l, r)的哈希值.
//  Combine(h1, h2, h2len) : 哈希值h1和h2的拼接, h2的长度为h2len. (线段树的op操作)
//  AddChar(h,c) : 哈希值h和字符c拼接形成的哈希值.
//  Lcp(aTable, l1, r1, bTable, l2, r2) : O(logn)返回a[l1, r1)和b[l2, r2)的最长公共前缀.

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[i+1]) + string(word[i])
			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