data structures and algorithms

39
Spring 2001 Data Structure Lecture no tes Data Structures and Data Structures and Algorithms Algorithms Lecture notes for String Lecture notes for String Jiang Lihong Jiang Lihong Shanghai Jiaotong Shanghai Jiaotong University University

Upload: devon

Post on 22-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Data Structures and Algorithms. Lecture notes for String Jiang Lihong Shanghai Jiaotong University. Main Topic. 4.1 串的基本概念 4.2 串的运算及其实现 4.3 串的存储结构 4.4 串的模式匹配. 4.1 串的基本概念. 串(或字符串),是由零个或多个字符组成的有限序列。一般记为: s='a1a2...an'(n>=0). 串的例子. S1=“ab123” // 长度为5的串 S2=“100” // 长度为3的串 - PowerPoint PPT Presentation

TRANSCRIPT

Spring 2001 Data Structure Lecture notes

Data Structures and AlgorithmsData Structures and Algorithms

Lecture notes for StringLecture notes for String

Jiang LihongJiang Lihong

Shanghai Jiaotong UniversityShanghai Jiaotong University

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

22

Main Topic Main Topic

• 4.1 4.1 串的基本概念 串的基本概念

• 4.2 4.2 串的运算及其实现 串的运算及其实现

• 4.3 4.3 串的存储结构 串的存储结构

• 4.4 4.4 串的模式匹配串的模式匹配

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

33

4.1 4.1 串的基本概念串的基本概念

• 串(或字符串),是由零个或多个字符组成的有串(或字符串),是由零个或多个字符组成的有限序列。一般记为:限序列。一般记为:

• s='a1a2...an'(n>=0)s='a1a2...an'(n>=0)

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

44

串的例子串的例子

S1=“ab123” //S1=“ab123” // 长度为长度为 55 的串的串S2=“100” //S2=“100” // 长度为长度为 33 的串的串S3=" " //S3=" " // 含两个空格字符的串长度为含两个空格字符的串长度为 22

S4=“” //S4=“” // 空串长度为空串长度为 00

a='BEI',b='JING',c='BEIJING',d='BEI JING'a='BEI',b='JING',c='BEIJING',d='BEI JING'• 串长分别为串长分别为 3,4,7,8,3,4,7,8, 且且 a,ba,b 都是都是 c,dc,d 的子串。的子串。• 称两个串是相等的,当且仅当这两个串的值相等称两个串是相等的,当且仅当这两个串的值相等

。。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

55

• a=` `a=` ` 称为空白串。它的长度为称为空白串。它的长度为 11 。由于空。由于空白串本身是一个字符,因此它可以出现在白串本身是一个字符,因此它可以出现在其它字符中间,例如“其它字符中间,例如“ bei jing”bei jing” 。。为清楚为清楚起见,下文中的空白字符用“起见,下文中的空白字符用“ σ”σ” 表示。表示。

• a=``a=`` 称为空串。它的长度为称为空串。它的长度为 00 。空串中无。空串中无任何字符任何字符

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

66

4.2 4.2 串的运算及实现串的运算及实现

• 串的抽象数据类型的定义:串的抽象数据类型的定义:• ADT String{ADT String{

• 数据对象:数据对象: D={ai|ai(-D={ai|ai(-CharacterSet,i=1,2,...,n,n>=0}CharacterSet,i=1,2,...,n,n>=0}

• 数据关系:数据关系: R1={<ai-1,ai>|ai-1,ai(-R1={<ai-1,ai>|ai-1,ai(-D,i=2,...,n}D,i=2,...,n}

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

77

• 基本操作:基本操作:• StrAssign(&T,chars) //////////StrAssign(&T,chars) //////////• charschars 是字符常量。生成一个其值等于是字符常量。生成一个其值等于 charschars 的串的串 TT 。。• StrCopy(&T,S)StrCopy(&T,S)• 串串 SS 存在则由串存在则由串 SS 复制得串复制得串 TT• StrEmpty(S)StrEmpty(S)• 串串 SS 存在则若存在则若 SS 为空串为空串 ,, 返回真否则返回假返回真否则返回假• StrCompare(S,T) ///////////StrCompare(S,T) ///////////• 串串 SS 和和 TT 存在存在 ,, 若若 S>T,S>T, 则返回值大于则返回值大于 0,0, 若若 S=T,S=T, 则返则返

回值回值 =0,=0, 若若 S<T,S<T, 则返回值则返回值 <0<0• StrLength(S) //////////StrLength(S) //////////• 串串 SS 存在返回存在返回 SS 的元素个数称为串的长度的元素个数称为串的长度 ..• ClearString(&S)ClearString(&S)• 串串 SS 存在将存在将 SS 清为空串清为空串

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

88

• Concat(&T,S1,S2) //////////////Concat(&T,S1,S2) //////////////• 串串 S1S1 和和 S2S2 存在用存在用 TT 返回由返回由 S1S1 和和 S2S2 联接而成的新串联接而成的新串• SubString(&Sub,S,pos,len) /////////////SubString(&Sub,S,pos,len) /////////////• 串串 SS 存在存在 ,1<=,1<=pos<=StrLength(S)pos<=StrLength(S) 且且 0<=0<=len<=StrLength(S)-pos+1len<=StrLength(S)-pos+1• Index(S,T,pos)Index(S,T,pos)• 串串 SS 和和 TT 存在存在 ,,TT 是非空是非空 ,1<=,1<=pos<=StrLength(S),pos<=StrLength(S), 若主串若主串 SS 中存在中存在

和串和串 TT 值相同的子串值相同的子串 ,, 则返回它在主串则返回它在主串 SS 中第中第 pospos 个字符之后第一个字符之后第一次出现的位置次出现的位置 ,, 否则函数值为否则函数值为 00

• Replace(&S,T,V)Replace(&S,T,V)• 串串 S,TS,T 和和 VV 存在存在 ,,TT 是非空串是非空串 ,, 用用 VV 替换主串替换主串 SS 中出现的所有与中出现的所有与 TT

相等的不重叠的子串相等的不重叠的子串• StrInsert(&S,pos,T)StrInsert(&S,pos,T)• 串串 SS 和和 TT 存在存在 ,1<=,1<=pos<=StrLength(S)+1,pos<=StrLength(S)+1, 在串在串 SS 的第的第 pospos 个字符个字符

之前插入串之前插入串 TT• StrDelete(&S,pos,len)StrDelete(&S,pos,len)• 串串 SS 存在存在 ,1<=,1<=pos<=StrLength(S)-len+1pos<=StrLength(S)-len+1 从串中删除第从串中删除第 pospos 个字符个字符

起长度为起长度为 lenlen 的子串的子串• DestroyString(&S)DestroyString(&S)• 串串 SS 存在存在 ,, 则串则串 SS 被销毁被销毁• }}ADT StringADT String

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

99

4.34.3 串的存储结构串的存储结构 (( 一一 ) ) 串的定长存串的定长存储储

• 用一组地址连续的存储单元存储串值的字符用一组地址连续的存储单元存储串值的字符序列序列 ..

• ##define MAXSTRLEN 255define MAXSTRLEN 255

• typedef unsigned char typedef unsigned char SString[MAXSTRLEN+1] //0SString[MAXSTRLEN+1] //0 号单元存放串号单元存放串长长

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1010

• 超过予定义长度的串值则被舍去超过予定义长度的串值则被舍去

• 串长可用下标为串长可用下标为 00 的数组元素存储的数组元素存储 ,, 也可也可在串值后设特殊标记在串值后设特殊标记

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1111

串联接的实现串联接的实现 Concat(&T,S1,S2)Concat(&T,S1,S2)

• 假设假设 S1,S2S1,S2 和和 TT 都是都是 SStringSString 型的串变量型的串变量 ,, 且串且串 TT是由串是由串 S1S1 联结串联结串 S2S2 得到的得到的 ,, 即串即串 TT 的值的前一的值的前一段和串段和串 S1S1 的值相等的值相等 ,, 串串 TT 的值的后一段和串的值的后一段和串 S2S2的值相等的值相等 ,, 则只要进行相应的则只要进行相应的 "" 串值复制串值复制 "" 操作即操作即可可 ,, 对超长部分实施对超长部分实施 "" 截断截断 "" 操作操作

• 以下是串联接可能出现的三种情况以下是串联接可能出现的三种情况 ::• S1,S2S1,S2 串长和小于最大值串长和小于最大值• S1,S2S1,S2 串长和超过最大串长串长和超过最大串长• S1S1 串长已等于最大串长串长已等于最大串长

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1212

• Status Concat(SString &T,SString S1,SString S2){Status Concat(SString &T,SString S1,SString S2){• if(S1[0]+S2[0]<=MAXSTRLEN){if(S1[0]+S2[0]<=MAXSTRLEN){• T[1..S1[0]]=S1[1..S1[0]];T[1..S1[0]]=S1[1..S1[0]];• T[S1[0]+1..S1[0]+S2[0]]=S2[1..S2[0]];T[S1[0]+1..S1[0]+S2[0]]=S2[1..S2[0]];• T[0]=S1[0]+S2[0]uncut=TRUE;T[0]=S1[0]+S2[0]uncut=TRUE;• }}• else if(S1[0]<MAXSTRSIZE){else if(S1[0]<MAXSTRSIZE){• T[1..S1[0]]=S1[1..S1[0]];T[1..S1[0]]=S1[1..S1[0]];• T[S1[0]+1..MAXSTRLEN]=S2[1..MAXSTRLEN-S1[0]];T[S1[0]+1..MAXSTRLEN]=S2[1..MAXSTRLEN-S1[0]];• T[0]=MAXSTRLEN;uncut=FALSE;T[0]=MAXSTRLEN;uncut=FALSE;• }}• else{else{• T[0..MAXSTRLEN]=S1[0..MAXSTRLEN];T[0..MAXSTRLEN]=S1[0..MAXSTRLEN];• uncut=FALSE;uncut=FALSE;• }}• return uncut;return uncut;• }}

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1313

(( 二二 ) ) 串的单链表存储串的单链表存储

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1414

(( 三三 ) ) 块链存储表示块链存储表示

• 可利用空间划分成大小一样的结点可利用空间划分成大小一样的结点 (( 比如比如说划分成大小为说划分成大小为 44 的结点的结点 )) ,每一个结点,每一个结点有两个域:有两个域: datadata 域放域放 44 个字符,个字符, linklink 域域放下一个结点的指针。例如放下一个结点的指针。例如 , , s='abcdefghk's='abcdefghk'

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1515

四、堆分配存储表示四、堆分配存储表示

• 动态分配一组连续的存储单元动态分配一组连续的存储单元• Malloc()Malloc()

• free()free()

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1616

4.3 4.3 模式匹配模式匹配

• 设设 ss 和和 tt 是给定的两个串,在串是给定的两个串,在串 ss 中找到中找到等于等于 tt 的子串的过程称为模式匹配。的子串的过程称为模式匹配。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1717

(( 一一 ) ) 模式匹配的模式匹配的 BFBF 算法算法

• 一种简单直观的模式匹配算法是布鲁特一种简单直观的模式匹配算法是布鲁特((Brute)-Brute)- 福斯福斯 ((Force)Force) 算法,简称算法,简称 BFBF 算法。算法。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1818

算法算法 4.54.5

• Int Index (Sstring S, Sstring T, int pos) {Int Index (Sstring S, Sstring T, int pos) {

• i=pos; j=1;i=pos; j=1;

• while (i<=S[0]&&j<=T[0]){while (i<=S[0]&&j<=T[0]){

• if (S[i]=T[j]) {++i:++j;}if (S[i]=T[j]) {++i:++j;}

• else {i=i-j+2; j=1;}else {i=i-j+2; j=1;}

• }}

• if (j>T[0]) return i-T[0];if (j>T[0]) return i-T[0];

• else return 0; }//Indexelse return 0; }//Index

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

1919

模式模式 t='cda't='cda' 与主串与主串 s='acdccdae's='acdccdae'

初始

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2020

模式模式 t='cda't='cda' 与主串与主串 s='acdccdae's='acdccdae'

(1) s1!=t1 , i <- 2, j <-1, 重新比

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2121

模式模式 t='cda't='cda' 与主串与主串 s='acdccdae's='acdccdae'

(2) s4!=t3 , i <-3 , j <-1 重新比

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2222

模式模式 t='cda't='cda' 与主串与主串 s='acdccdae's='acdccdae'(3) s3!=t1 , i <-4 , j <-1 重新比

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2323

模式模式 t='cda't='cda' 与主串与主串 s='acdccdae's='acdccdae'

(4) s5!=t2 , i <-5, j <-1, 重新比

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2424

模式模式 t='cda't='cda' 与主串与主串 s='acdccdae's='acdccdae'

这个算法很简单,但是效率很低。

算法最坏的运行时间是 O(m*n) 。

造成 BF 算法速度慢的原因是回溯,而这些回溯并不是必要的。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2525

(( 二二 )) 模式匹配的模式匹配的 kmpkmp 算法算法

• 克努特克努特 ((Knuth)Knuth) ,,莫里斯莫里斯 ((Morris)Morris) 和普拉特和普拉特 ((Pratt)Pratt)

• 造成造成 BFBF 算法速度慢的原因是回溯,而这算法速度慢的原因是回溯,而这些回溯并不是必要的。希望在每趟匹配后些回溯并不是必要的。希望在每趟匹配后,指针,指针 ii 不回溯,由不回溯,由 jj 退到某一个位置退到某一个位置 kk上,使上,使 tt 中中 kk 前的前的 k-1k-1 个字符与个字符与 ss 中中 ii 指针指针前的前的 k-1k-1 字符相等。这将减少匹配的趟数字符相等。这将减少匹配的趟数(( 和一趟比较的次数和一趟比较的次数 )) ,提高算法的效率,提高算法的效率。 如何得到。 如何得到 kk 值是改进的模式匹配算法的值是改进的模式匹配算法的关键。关键。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2626

• KnuthKnuth 等人发现这个等人发现这个 kk 值仅依赖与模式值仅依赖与模式 tt本身前本身前 jj各字符的构成,而与主串各字符的构成,而与主串 ss 无关无关,且可用一个,且可用一个 next(j)next(j) 表示与表示与 jj 对应的对应的 kk 值值。若令。若令 next(j)=knext(j)=k ,,则则 next(j)next(j) 表明,当模表明,当模式式 tt 的第的第 jj 个字符与主串相应字符匹配失个字符与主串相应字符匹配失败时,需重新和主串该字符进行比较的字败时,需重新和主串该字符进行比较的字符位置。即当符位置。即当 ssii!=t!=tjj 时,对模式向右移时,对模式向右移 j-j-

next(j)next(j) 字符,与从起主串续比较下去。若字符,与从起主串续比较下去。若next(j)=0next(j)=0 ,,则则 jj移到移到 tt 的第一个字符的第一个字符 ,, 与与从从 ssi+1i+1 起的主串续比较起的主串续比较

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2727

算法 算法 4.6 4.6 模式匹配的模式匹配的 KMPKMP 算法算法((s,t,pos)s,t,pos) 思路思路

• 1) 1) i <- 1, j <- 1i <- 1, j <- 1

• 2 )2 ) 循环 当循环 当 i <= m i <= m 且 且 j <= n j <= n 时执行时执行• 若 若 s(i)=t(j)s(i)=t(j)

• 则 则 i <- i+1, j <- j+1i <- i+1, j <- j+1

• 否则 若 否则 若 next(j)>0next(j)>0

• 则 则 j <- next(j)j <- next(j)

• 否则 否则 j <-1, i <-i+1j <-1, i <-i+1

• 3) 3) 若 若 j>n j>n 则输出 则输出 i-ni-n 否则 输出 否则 输出 ''0'0'

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2828

• 在算法在算法 4.64.6 中,中, ii 值只增不减,且值只增不减,且 ii 初值为初值为11循环过程又控制在循环过程又控制在 i<=mi<=m ,,因此循环体因此循环体中语句中语句 i <- i+1i <- i+1 最多执行最多执行 mm 次,所以该算次,所以该算法运行时间为法运行时间为 O(m)O(m) 。。

• 上述算法还遗留一个问题,即如何计上述算法还遗留一个问题,即如何计算算 next(j)next(j) 。。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

2929

• next(j)next(j) 是一个满足于的整数是一个满足于的整数 ..

• next(j)next(j) 的值的值 kk ,,应使应使 tt 中中 kk 前的前的 k-1k-1 个字个字符与符与 ss 中中 ii 指针前的指针前的 k-1k-1 个字符相等。个字符相等。

• 所取所取 kk 值,应使值,应使 tt 的右移不丢失任和匹配的右移不丢失任和匹配成功的可能固在存在多个满足性质成功的可能固在存在多个满足性质 (2)(2) 的的kk 时,取最大的时,取最大的 kk 。。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3030

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3131

• ' ' t t 11 ... t ... t k-1k-1 ' = ' t ' = ' t j-k+1j-k+1 ... t ... t j-1j-1 ' (4-1) ' (4-1)

• 因为由性质因为由性质 (2)(2) 所示所示 ,,我们可得到两个关系式我们可得到两个关系式 ::

• ' ' t t 11 ... t ... t k-1k-1 ' = ' s ' = ' s i-k+1i-k+1 ... s ... s i-1i-1 ' (4-2) ' (4-2)

• ' t ' t j-k+1j-k+1 ... t ... t j-1j-1 ' = 's ' = 's i-k+1i-k+1 ... s ... s i-1i-1 ' (4-3) ' (4-3)

• 那么,由式那么,由式 (4-2)(4-2) 和和 (4-3)(4-3)便可得到便可得到 (4-1)(4-1) 。。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3232

计算 计算 next next 值算法值算法

• 1 1 k <-0, j<-1, next(1) <- 0k <-0, j<-1, next(1) <- 0

• 2 2 循环 执行下列语句循环 执行下列语句 ,, 直到 直到 j=0 j=0 为止为止• 若 若 k=0 k=0 或 或 t j=t kt j=t k

• 则 则 j <- j+1, k<- k+1, next(j) <- kj <- j+1, k<- k+1, next(j) <- k

• 否则 否则 k <- next(k)k <- next(k)

• {{ 算法结束算法结束 }}

• 这个算法的时间复杂度为这个算法的时间复杂度为 O(n)O(n) 。。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3333

• 一 判断题一 判断题 ((y/n)y/n)

• 1,1, 子串定位函数的时间复杂度在最坏情况子串定位函数的时间复杂度在最坏情况下为下为 OO (( n*mn*m ),),因此子串定位函数没因此子串定位函数没有实际使用价值。 有实际使用价值。

• 2, 2, 设有两个串设有两个串 pp 和和 qq ,,其中其中 qq 是是 pp 的子的子串,把串,把 qq 在在 pp 中首次出现的位置作为子串中首次出现的位置作为子串qq 在在 pp 中的位置的算法称为匹配。中的位置的算法称为匹配。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3434

• 3, 3, KMPKMP 算法的最大特点是指示主串的指算法的最大特点是指示主串的指针不需回溯。针不需回溯。

• 4, 4, 设模式串的长度为设模式串的长度为 mm ,,目标串的长度目标串的长度为为 nn ,,当当 n≈mn≈m 且处理只匹配一次的模式且处理只匹配一次的模式

时,朴素的匹配(既子串的定位函数)算时,朴素的匹配(既子串的定位函数)算法所花的时间代价也可能会更为节省。法所花的时间代价也可能会更为节省。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3535

二 单选题 二 单选题 ((请从下列请从下列AA ,, BB ,, CC ,, DD选项中选择一项)选项中选择一项)• 11 ,设字符串,设字符串

s1='ABCDEFG',s2='PQRST's1='ABCDEFG',s2='PQRST' ,,则运算则运算s=CONCAT(SUB(s1,2,LEN(s2)),SUB(s1,s=CONCAT(SUB(s1,2,LEN(s2)),SUB(s1,LEN(s2),2))LEN(s2),2)) 后的串值为:后的串值为:

• ‘‘BCDEF’ BCDEF’

• ‘‘BCDEFG’ BCDEFG’

• ‘‘BCPQRST’ BCPQRST’

• ‘‘BCDEFEF’ BCDEFEF’

• ‘‘BCQR’ BCQR’

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3636

• 22 ,设有两个串 ,设有两个串 p p 和 和 q ,q , 求 求 q q 在 在 p p 中中首次出现的位置的运算:首次出现的位置的运算:

• 连接 连接 • 模式匹配 模式匹配 • 求子串 求子串 • 求串长 求串长

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3737

三 编程题三 编程题• 1.1. 设设 xx 和和 yy 是表示成单链表的两个串,试写出一个算法是表示成单链表的两个串,试写出一个算法

,找出,找出 xx 中第一个不在中第一个不在 yy 中出现的字符(假定每个结点中出现的字符(假定每个结点只存放一个字符)。只存放一个字符)。

• 2.2. 试设计在顺序串上实现串的比较运算试设计在顺序串上实现串的比较运算 strcmp(s,t)strcmp(s,t) 的算的算法。法。

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3838

• PROCEDURE ds0405(x,y : ctr;VAR p : ctr);PROCEDURE ds0405(x,y : ctr;VAR p : ctr);• VAR px,py : ctr;VAR px,py : ctr;• BEGIN BEGIN • px : = x;px : = x;• WHILE px <> NIL DOWHILE px <> NIL DO• BEGINBEGIN• py : = y;py : = y;• WHILE (py <> NIL) AND (px↑.data <> py↑.data) DOWHILE (py <> NIL) AND (px↑.data <> py↑.data) DO• py : = py↑.link;py : = py↑.link;• IF py = NIL THEN px : = NILIF py = NIL THEN px : = NIL• ELSE BEGINELSE BEGIN• px : = px↑.link; x : = pxpx : = px↑.link; x : = px• ENDEND• END;END;• p : = x;p : = x;• END;END;

Spring 2001Spring 2001 Data Structure Lecture noteData Structure Lecture notess

3939

设串 x 和 y 分别存放在向量 A[1..maxsize] 的前 m 个分量和B[1..maxsize] 的前 n 个分量中,且 0 < m , n < maxsize 。

FUNCTION ds0406(A,B : ARRAY[1..maxsize] of char; m,n : 1..maxsize) : -1..1;

VAR s,i : integer;BEGIN

IF m < n THEN s : = m {s = min{m,n}} ELSE s : = n;

i : = 1;WHILE i <= s DO

IF A[i] < B[i] THEN RETURN (-1) {x < y}

ELSE IF A[i] > B[i] THEN RETURN(1) {x > y}

ELSE IF i = s THEN IF m = n

THEN RETURN(0) {x = y} ELSE IF m > n

THEN RETURN(1) {x > y} ELSE RETURN(-1) {x < y}

END;