【問題】
假設一字符串已被預處理,其中出現過的每一字符其出現的位置已被存到已排序列表(sorted list)中,例如
A = "this is a text"
預處理後會得到一下列表:
a : 9
e : 12, 17
h : 2
i : 3, 6
s : 4, 7, 13
t : 1, 11, 14, 16, 19
x : 18
空格 : 5, 8, 10, 15
對任意給定字符串B,請設計一算法找出字符串A中B出現的所有位置,算法複雜度多少?
【思想】
預處理主要是爲了效率高,節省時間複雜度。稍後詳解。
【代碼】
【輸出】
Debug/PretreatmentStringAllocate.exe
Sample String: okooook,ok okokoooooooo
Template String: ok
***Now pretreating the string:okooook,ok okokoooooooo
***Letters Table as the string:
: 11,12
,: 8
k: 2,7,10,14,16
o: 1,3,4,5,6,9,13,15,17,18,19,20,21,22,23,24
Position is:
1 6 9 13 15
D:/WorkSpaceSlickEdit/PretreatmentStringAllocate>