東京大学 情報理工学系研究科 コンピュータ科学専攻 2019年8月実施 専門科目I 問題3
Author
祭音Myyura (co-authored with GPT 5.6 SOL)
Description
In this problem, the length of a string is written , and the -th character of is written , where the first character is . The string obtained by removing the first characters from is written . We assume in and . For example, if , then and . The set of characters consists of characters, where is an integer constant no less than 2, and for each character a distinct positive integer is defined. Suppose that the computation of for given and , and that of for given , take time. Also suppose that each of integer addition, multiplication and remainder takes time, and that overflow will never occur in integer operations.
We consider the following problem FIND: For given strings and , find the first position at which matches . In other words, is the least non-negative integer that satisfies
In case there is no such , we define . In the following, we assume .
For strings and with , let function return 1 if the first characters of equal , and return 0 otherwise. Suppose that the time complexity of is . The following algorithm solves the problem FIND:
for (i = 0; i <= l(s) - l(p); i++)
if (eq(s + i, p) == 1)
return i;
return -1;
Answer the following questions.
(1) Express the order of the worst-case time complexity of algorithm in terms of and .
In the following, the hash value of the first characters of string is defined by
where and are positive integer constants, and is assumed.
(2) Assume that holds, and that and have been precomputed. Show an algorithm or an expression to compute in time.
(3) Give an algorithm that finds the least non-negative integer that satisfies (but answers if no such exists) in time . Also, answer in what condition the algorithm outputs a value which is not the solution of problem FIND.
(4) Give an algorithm that satisfies all of the following conditions: (a) it always answers the solution of problem FIND, (b) it searches for the answer by using hash and function , and () if we assume that the number of integers that satisfy for given and is independently of and , then the algorithm runs in time . In addition, show in what condition the time complexity of the algorithm is larger than . Also, answer the order of the worst-case time complexity of the algorithm in terms of and .
题目描述
字符串 的长度记为 ,从 开始编号的第 个字符记为 ,删去前 个字符所得后缀记为 ,并在这些记号中假设 。例如 时, 、。字符集含常数 个字符,每个字符 对应互不相同的正整数 。假设计算 、、整数加法、乘法和取余均为 ,且整数运算不会溢出。
问题 FIND 要求:给定字符串 ,求 在 中第一次匹配的起点,即满足
的最小非负整数 ;若不存在则返回 。以下假设 。对满足 的字符串,函数 在 的前 个字符等于 时返回 ,否则返回 ,耗时 。题中朴素算法 从左到右对每个位置调用该函数。
(1)用 表示算法 的最坏时间复杂度。
对满足 的 ,定义前 个字符的哈希:
其中 为正整数常数。
(2)设 ,且已预计算 和 。给出在 时间内计算 的算法或表达式。
(3)给出算法 ,在 时间内找出满足 的最小非负 ;若不存在则返回 。并说明在什么条件下 的输出不是 FIND 的正确答案。
(4)给出算法 ,满足:始终正确求解 FIND;使用哈希 和函数
eq 搜索;若对给定 ,哈希相等的候选位置数与输入无关地为
,则总耗时为 。此外说明何种条件会使其耗时超过这一界,并用
给出最坏时间复杂度。
Kai
记 。
(1)
共有 个起点,每次比较至多 个字符,故最坏时间为
若 eq 采用逐字符比较,取 可达到 。
(2)
令 为 中的标准余数,则
此式删除最左字符的贡献、将其余项乘 ,再加上新字符,耗时 。若语言的 % 会返回负数,使用 ((x % q) + q) % q 实现标准余数。
(3)
用 Horner 递推 v = mod_q(v*d + numval(c)) 求两个初始哈希,并用 次乘法求 。
hp = h(p,m); hs = h(s,m)
for i = 0,...,n-m:
if hs == hp: return i
if i < n-m:
hs = mod_q(d*(hs-numval(s[i])*d_m)+numval(s[i+m]))
return -1
初始化为 ,每次滚动为 ,故共 。
错误当且仅当它找到的第一个哈希相等窗口并不等于 。若没有哈希相等窗口,则必无真正匹配,返回 正确。
(4)
将上面算法的返回条件改成
if hs == hp and eq(s+i,p) == 1: return i
便得到 。真正相等的串哈希一定相等,且扫描按起点递增进行,因此总能返回首个真正匹配。
设返回前实际检查了 个候选窗口,总时间为
若 ,即为 。当大量碰撞引发长字符比较,使比较总耗时超过线性界时,算法退化。例如允许的常数 使所有窗口碰撞,取 ,逐字符 eq 每次都比较 个字符。
因此最坏上界为 ,逐字符比较下上述例子达到该界;常见宽松写法为 。仅有许多哈希相等位置并不足以断言退化,因为算法可能很早就返回。