TAILIEUCHUNG - Mastering Algorithms with Perl phần 6

Nhưng điều này là ngớ ngẩn: xuất hiện n $ c, (n là vô hướng @ S, kích thước của @ S) này thực hiện n bổ sung và phép nhân 2n. Thay vào đó chúng ta có thể nhận được ngay với chỉ có n phép nhân (và $ sức mạnh là không cần thiết ở tất cả): | foreach c @S sum c power power Sigma Or rather the code shows the iterative formulation of it the more mathematically minded may prefer crxr cr-lxr-1 . . . c2x2 c1x c0 . . . crx cn1 x . . . x c1 x c0. Page 364 But this is silly for r occurrences of c r is scalar @S the size of @S this performs r additions and 2r multiplications. Instead of that we can get away with only r multiplications and the power is not needed at all sum 0 foreach c @S sum Sigma sum c This trick is the Horner s rule. Within the loop perform one multiplication instead of the two first and then one addition. We can further eliminate one of the multiplications the useless multiplication of zero sum S 0 foreach c @S 1. S sum Sigma sum c So from 2r 2 assignments counting and as assignments r additions and 2r multiplications we have reduced the burden to 2r - 1 assignments r - 1 additions and r - 1 multiplications. Having processed the pattern we advance through the text one character at a time processing each slice of m characters in the text just like the pattern. When we get identical numbers we are bound to have a match because there is only one possible combination of multipliers that can produce the desired number. Thus the multipliers characters in the text are identical to the multipliers in the pattern. Handling Huge Checksums The large checksums cause trouble with Perl because it cannot reliably handle such large integers. Perl guarantees reliable storage only for 32-bit integers covering numbers up to 232 -l. That translates into 4 8-bit characters. After that number Perl silently starts using floating point numbers which cannot guarantee exact storage. Large floating point numbers start to lose their less significant digits making tests for numeric equality useless. Rabin and Karp proposed using modular arithmetic to handle these large numbers. The checksums are computed in modulo q. q is a prime such that E l q is still below the maximum integer the system can handle. More specifically

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.