1 / 15

# 第四章 串 - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about '第四章 串' - saniya

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 第四章 串

• 串类型的定义
• 串的表示和实现
• 串的模式匹配算法
• 串操作应用举例

4.1串类型的定义

• V是程序设计语言所使用的字符集，由V上的字符组成的序列称作字符串。
• 空串
• 串的长度
• 子串
• 串的存贮

4.2 串的表示和实现

• 串的运算

• #define MAXN 100
• typedef enum { fail, success } status;
• typedef enum { false, true} boolean;
• char s[MAXN], s1[MAXNj, s2[MAXN];
• int strlen ( char s[ ] )
• { int i;
• for (i=0; s[i]!='\0'; i++ );
• return (i);
• }

• status strcat (s1, s2)
• char s1[ ], s2[];
• { int i, j, k;
• if ((i=strlen (s1)) + (j=strlen (s2) )>=MAXN)
• return (fail);
• for (k=0; k<=j; k++ )
• s1[i+k]=s2[k];
• return (success);
• }

status strsub (char s1[ ],

int i, int j, char s2[ ] )

• { int m, k;
• if ( i<0 || i>= (m=strlen(s1) ) )
• return (fail );
• if (j<0 || i+j>m) return (fail );
• for (k=0; k<j; k++ )
• s2[k]=s1[i +k];
• s2[k]='\0';
• return (success );
• }

4.3 串的模式匹配算法

• 模式匹配的简单算法
• #define MAXN 100
• #define MAXM 30
• char t[MAXN], p[MAXM];
• int simple_ match (t, p, n, m)
• char t[ ], p[ ];
• int n, m;
• { int i, j, k;
• for (i=0; i<=n-m;i++)
• { for (j=0, k=i; j<m && t[k]==p[j]; k++, j++);
• if (j= =m) return(i);
• }
• return(- 1);
• }
2．模式匹配的KMP(Kunth Morris Pratt)方法
• #define MAXN 100
• #define MAXM 20
• char t [MAXN], p [MAXM];
• int m, n;
• { int j, k;
• flink [0] = - 1;
• j=1;
• while (j<m)
• while ( k !=-1 && p[k]!=p[j-1] )
• flink [j] = k + 1;
• j++; }
• }
int kmp_match(char t[ ], char p[ ],
• int flink[ ], int n, int m)
• { int i, j;
• i= 0;
• j= 0;
• while (i<n)
• { while (j!=-1 && p[j]!=t[i])
• if (j=m-1) return(i-m+1);
• i+ +; j+ +;
• }
• return (-- 1 );
• }
#define MAXN 100
• #define MAXM 20
• char t[MAXN], p[MAXM];
• int n, m;
• int d[26);
• void df(p, d, m)
• char p[ ];
• int d[ ], m;
• { int i;
• for (i=0; i<26; i+ + )
• d[i] =m;
• for (i= 0; i<m-1 ; i++ )
• d[P[i]-'a']=m-i-1;
• }
int bm_match(t, p, d, n, m)
• char t[],p[];
• int d[], n,m;
• { int i, j, k;
• i=m-1;
• do { j=m-1;
• k=i;
• while (j>=0 && p[j]==t[k])
• { j--;
• k--;
• }
• if (j<0) return (i-m+1);
• i=i+d[t[i]-'a'];
• } while (i<n);
• return (-1 );
• }
4.1 串操作应用举例
• 包括串的插入、删除等基本操作。

• status strins (s1, i, s2)
• char s1[ ], s2[ ]; int i ;
• { int m, n, k;
• if (i<0 || i> ( m=strlen(s1) )
• || m+( n=strlen (s2 ) ) >MAXN)
• return ( fail );
• for (k=m; k>=i; k--) s1[k+n] = s1[k];
• for (k=0; k<n; k++ ) s1[i+k] = s2[k];
• return (success );
• }

• status strdel (s, i, j)
• char s[ ];
• int i,j;
• { int m, k;
• if (i<0|| i >= ( m=strlen(s) ) )
• return (fail);
• if (i+j>=m) s[i]='\0';
• else for (k=0; k<=m-i-j;k++)
• s[i+k]=s[i+j+k];
• return (success);
• }

• boolean strequ (s1, s2)
• char s1[ ], s2[ ];
• { int i=0;
• while (s1[i]= =s2[i]&& s1[i]!='\0'
• && S2[i]!='\0')
• i++;
• if ( s1[i]=='\0' && s2[i]=='\0‘ )
• return ( true ) ;
• else return ( false );
• }

• 1.输入一段正文，统计其中字母．数字和其它字符的个数。
• 2.写出函数substring(s,n,f,l,sub)的算法，从长度为n的字符串S中找出首末字符的序号分别为f,l的子串放到sub中。
• 3.求出字符串”abcabcabc”的失败链接值。
• 4.Converse(s) 将字符串s的内容颠到次序存放。
• 5.写出函数replace(s1,s2,s3,n,ll,m)的算法，将s1中所有s2的出现都置换成s3,n,l,m分别是字符串s1,s2,s3的长度.