Strings

Embed Size (px)

DESCRIPTION

Strings in theory of Computation

Citation preview

  • S4 g, Theory ofComputer Science

    (b) Which vertices are the sons of 57(c) Which vertex is the father of 57(d) \Xlhat is the length of the path from 1 to 97(e) What is the left-right order of leaves?(f) What is the height of the tree?

    2

    10

    7 8

    oFig. 2.13 The directed tree for Example 2.18.

    Solutions(a) 10, 4, 9, 8, 6 are leaves. 1, 2. 3, 5, 7 are internal vertices.(bi 7 and 8 are the sons of 5.(c) 3 is the father of 5.(d) Four (the path is 1 ~ 3 ~ 5 ~ 7 ~ 9).(e) 10 - 4 - 9 - 8 - 6.(f) Four (1 ~ 3 ~ 5 ~ 7 ~ 9 is the longest path).

    2.3 STRINGS AND THEIR PROPERTIESA string over an alphabet set 2: is a finite sequence of symbols from 2:.

    NOTATION: 2:* denotes the set of all strings (including A, the empty string)over the alphabet set 2:. That is, 2:+ = 2:* - {A}.

    2.3.1 OPERATIONS ON STRINGSThe basic operation for strings is the binary concatenation operation. Wedefine this operation as follows: Let x and y be two strings in 2:*. Let us forma new string :: by placing y after x, i.e. z = xy. The string z is said to beobtained by concatenation of x and y.

  • Chapter 2: Maihematical Preliminaries ~ 55

    EXAMPLE 2.1 9

    Find J:::V and yx, where(a) x = 010,(b) x = a1\.,

    y = 1y = ALGOL

    Solution(a) X)' = 0101, yx = 1010.(b) xy =a /\ i\LGOL

    yx = ALGOL aA.We give below some basic properties of concatenation.

    Property 1 Concatenation on a set 12* is associative since for each x, y, ::: in12*, x(y:::) = (xy):::.Property 2 Identity element. The set 12* has an identity element A W.r.t.the binary operation of concatenation as

    x1\. = Ax = x for every x in 12*

    Property 3 12* has left and right cancellations. For x, y, Z in 12*,z:x- = ;:;y implies x = y (left cancellation)x:: = yz implies x = y (right cancellation)

    Property 4 For x, y in 12*, we have

    Ixyl = Ixl + Iylwhere Ixi, Iy , . 'xv i denote the lengths of the strings x, y, xy, respectively.

    We introduce below some more operations on strings.

    Transpose OperationWe extend the concatenation operation to define the transpose operation asfollows:

    For any x in 12* and a in 12,(xayT = a(x)T

    For example. (aaabab;T is babaaa.Palindrome. A palindrome is a string which is the same whether written

    forward or backward, e.g. Malayalam. A palindrome of even length can beobtained by concatenation of a string and its transpose.

    Prefix and suffix of a string. A prefix of a string is a substring of leadingsymbols of that string. For example, w is a prefix of y if there exists y' in 12*such that y = Hy'. Tuen we write w < y. For example, the string 123 has fourprefixes, i.e. A. L 12, 123.

    Similarly, a suffix of a string is a substring of trailing symbols of thatstring, i.e. w is a suffix of y if there exists y' E 12* such that y = y'w. Forexample, the string 123 has four suffixes, i.e. 1\., 3, 23, 123.

  • 56 l;;\ Theory ofComputer Science

    Theorem 2.6 (Levi's theorem) Let v, W, x and Y E 1:* and vw =Ay. Then:(i) there exists a unique string z in 1:* such that v = xz and y = zw if

    11'1> Ixl;(ii) v = x, Y = w, i.e. z = A if Iv I = 1x I;

    (iii) there exists a unique stling z in 1:* such that x = vz, and = zy ifII'I < 14

    Proof We shall give a very simple proof by representing the strings by adiagram (see Fig. 2.14).

    t==x--...------y-----~Case 1: Ivl > I.xl v=xZ y =zw

    I1-_:===:===:::.==;==::1Case 2: Ivl =!xi v=x W =Y

    --

    Case 3: Ivl < Ixl x = vz w = zyFig. 2.14 Illustration of Levi's theorem.

    2.3.2 TERMINAL AND NONTERMINAL SYMBOLSThe definitions in this section will be used in subsequent chapters.

    A tenninal symbol is a unique indivisible object used in the generation ofstrings.

    A nonterminal symbol is a unique object but divisible, used in thegeneration of strings. A nonterminal symbol will be constructed from theterminal symbols: the number of terminal symbols in a nontenninal symbolmay vary; it is also called a variable. In a natural language, e.g. English, theletters a, b, A, B, etc. are terminals and the words boy, cat, dog, go arenonterrninal symbols. In programming languages, A, B, C, ... , Z, :, =, begin,and. if, then, etc. are terminal symbols.

    The following will be a variable in Pascal:

    < For statement > .~ for < control variable > =< for list > do < statement >

    Strings1.pdfBinder1scan0001

    Strings2.pdfBinder1scan0001

    Strings3.pdfBinder1scan0001