Lec 3
Lec 3
Fang Yu
¡ Example of alphabets:
¡ ASCII
¡ Unicode
¡ {0, 1}
¡ {A, C, G, T}
Strings
¡ Let P be a string of size m
¡ A substring P[i .. j] of P is the subsequence of P consisting
of the characters with ranks between i and j
¡ A prefix of P is a substring of the type P[0 .. i]
¡ “Fan” is a prefix of “Fang Yu, NCCU”
Operation Output
a.length()
a.charAt(1)
a.startsWith(“Hell”)
a.endsWith(“rld”)
a.substring(1,2)
a.concat(“rld”)
a.substring(1,2).equals(“e”)
indexOf(“rld”)
Java String Class
String a = “Hello World!”;
Operation Output
a.length() 12
a.charAt(1) e
a.startsWith(“Hell”) true
a.endsWith(“rld”) false
a.substring(1,2) e
a.concat(“rld”) Hello World!rld
a.substring(1,2).equals(“e”) true
a.indexOf(“rld”) 8
Java StringBuffer Class
StringBuffer S;
¡ Mutable strings: operations modify the strings
Operation a
a.append(“Hello World!”)
a.reverse()
a.reverse()
a.insert(6,”Fang and the ”)
a.setCharAt(4, ‘!’)
Java StringBuffer Class
StringBuffer a = new StringBuffer();
Operation a
a.append(“Hello World!”) Hello World!
a.reverse() !dlroW olleH
a.reverse() Hello World!
a.insert(6,”Fang and the ”) Hello Fang and the World!
a.setCharAt(4, ‘!’) Hell! Fang and the World!
Pattern Matching
¡ Given a text string T of length n and a pattern string P of
length m
¡ Applications:
¡ Text editors, Search engines, Biological research
Brute-Force Pattern Matching
The idea:
¡ Compare the pattern P with the text T for each possible
shift of P relative to T, until
¡ either a match is found, or
1. Backward comparison
T a p a t t e r n m a t c h i n g a l g o r i t h m
1 3 5 11 10 9 8 7
P r i t h m r i t h m r i t h m r i t h m
2 4 6
r i t h m r i t h m r i t h m
¡ Example:
¡ Σ = {a, b, c, d} c a b c d
¡ P = abacab L(c) 4 5 3 -1
Last Occurrence Function
¡ The last-occurrence function can be represented by an array
indexed by the numeric codes of the characters
. . . . . . a . . . . . . . . . . . . a . . . . . .
i i
. . . . b a . a . . b .
j l l j
m−j m − (1 + l)
. . . . b a . a . . b .
j 1+l
Another Example
a b a c a a b a d c a b a c a b a a b b
1
a b a c a b
4 3 2 13 12 11 10 9 8
Case 2 a b a c a b a b a c a b
5 7
Case 1 a b a c a b a b a c a b
6
a b a c a b
Is it a better algorithm?
¡ Boyer-Moore’s algorithm runs in time O(nm + s)
¡ For example:
¡ Enter URL: http://soslab.nccu.edu.tw
¡ Enter Keyword: Fang
¡ Output: Fang appears X times
Hints
Count A Keyword in a Web Page!
¡ Read TB Chapter 3