Алгоритм Колусси — различия между версиями
Kabanov (обсуждение | вклад) |
Kabanov (обсуждение | вклад) |
||
| Строка 25: | Строка 25: | ||
==Псевдокод== | ==Псевдокод== | ||
| + | |||
| + | '''Наивный вариант''' | ||
| + | '''int'''[] buildHmax('''char'''[] x, '''int''' m): | ||
| + | '''int''' hmax[m + 1] | ||
| + | '''for''' k = 1 .. m | ||
| + | '''int''' i = k | ||
| + | '''while''' x[i] == x[i - k] | ||
| + | i++ | ||
| + | hmax[k] = i | ||
| + | return hmax | ||
| + | |||
| + | '''Улучшенный вариант''' | ||
| + | '''int'''[] buildHmax('''char'''[] x, '''int''' m): | ||
| + | '''int''' hmax[m + 1] | ||
| + | '''int''' i = 1 | ||
| + | '''int''' k = 1 | ||
| + | '''while''' k <= m | ||
| + | '''while''' x[i] == x[i - k] | ||
| + | i++; | ||
| + | hmax[k] = i | ||
| + | '''int''' q = k + 1 | ||
| + | '''while''' hmax[q - k] + k < i | ||
| + | hmax[q] = hmax[q - k] + k | ||
| + | q++ | ||
| + | k = q | ||
| + | if k == i + 1 | ||
| + | i = k | ||
| + | '''return''' hmax | ||
| + | |||
==Асимптотики== | ==Асимптотики== | ||
* partitions the set of pattern positions into two disjoint subsets; the positions in the first set are scanned from left to right and when no mismatch occurs the positions of the second subset are scanned from right to left; | * partitions the set of pattern positions into two disjoint subsets; the positions in the first set are scanned from left to right and when no mismatch occurs the positions of the second subset are scanned from right to left; | ||
Версия 23:22, 14 мая 2014
Содержание
Алгоритм
Отметим, что нумерация символов строк и элементов массива у нас начинается с .
Сначала построим массивы: Kmp, Kmin, Rmin, Shift.
| Определение: |
| Обозначим за функцию, возвращающую количество прыжков для позиций , которые являются полными. |
| Определение: |
| Обозначим за функцию, возвращающую наименьший из периодов шаблона, которые больше позиции , для позиций, которые являются пустыми. |
| Определение: |
| Функция сдвига |
Псевдокод
Наивный вариант
int[] buildHmax(char[] x, int m):
int hmax[m + 1]
for k = 1 .. m
int i = k
while x[i] == x[i - k]
i++
hmax[k] = i
return hmax
Улучшенный вариант
int[] buildHmax(char[] x, int m):
int hmax[m + 1]
int i = 1
int k = 1
while k <= m
while x[i] == x[i - k]
i++;
hmax[k] = i
int q = k + 1
while hmax[q - k] + k < i
hmax[q] = hmax[q - k] + k
q++
k = q
if k == i + 1
i = k
return hmax
Асимптотики
- partitions the set of pattern positions into two disjoint subsets; the positions in the first set are scanned from left to right and when no mismatch occurs the positions of the second subset are scanned from right to left;
- preprocessing phase in time and space complexity;
- searching phase in time complexity;
- performs text character comparisons in the worst case.