- 6
- 0
- 约8.91千字
- 约 46页
- 2017-01-03 发布于重庆
- 举报
§4.1 String Concepts String: a sequence which consists of zero or more letters. s=‘a1a2…an’ (n≥0) Jargons: S, string name ‘a1a2…an’, value n, string length ai,letter or character n=0, Null String),noted as Ф ai = ‘ ’,blank string concepts Substring Substring’s position Example: s = ‘I am a chinese’; substring:s1 = ‘am’, s2 = ‘chinese’; position: 3 8 String’s ADT ADT String { objects:D={ai|ai∈CharacterSet,i=1,2,…,n n≥0} relation:R1={ ai-1,ai|ai-1,ai∈D,i=1, 2, …, n} operations: StrEmpty( S); StrCompare( S, T); index(T, S); String’s ADT StrLength( S); ClearString( S ); Concat( T, S1, S2 ); SubString( Sub, S, pos, len); Index( S, T, pos); Replace( S, T, V); StrInsert( S, pos, T); StrDelete( S, pos, len); DestroyString(S); } representation Sequential representation Linked representation Sequential representation #define MAXSTRING 255 typedef unsigned char SString [ MAXSTRLEN +1 ]; //element 0 stores the length of the string Concat( ) Status Concat( SString T, SString S1, SString S2 ) { if( S1[0] + S2[0] = MAXSTRLEN ) { T[ 1, … , S1[0] ] = S1[1, … S1[0]]; T[ S1[0] + 1 , … , S1[0] + S2[0] ] = S2[1, …, S2[0] ]; T[0] = S1[0] + S2[0]; return TRUE; } else if( S1[0] MAXSTRLEN ) { T[ 1, … , S1[0] ] = S1[1, … S1[0]]; T[ S1[0]+1, … , MAXSTRLEN ] = S2[1, … , MAXSTRLEN–S1[0] ]; T[0] = MAXSTRLEN; return FALSE; } else { T[ 1, … , S1[0] ] = S1[1, … S1[0]]; T[0] = MAXSTRLEN; return FALSE; } }// Concat Linked 串的模式匹配 The problem Given a text T[1], T[2], ..., T[n]and a pattern P[1], P[2], ..., P[m] Find all positions s such that P “occurs” in T at position s:(T[s], T[s+1], ..., T[s+m-1]) = (P[1], ..., P[m]) Where do we need this ? text editors (e.g. emacs) XML processing String Matching Example: Naive String Matching Naive String Matching Naive String Matching 主串:S=‘ababcabcacbab’模式串 :t=‘abcac’ 1 2 3 4 5 6 Knuth-Morris-Pratt(KMP) String Matching main idea: reuse the
原创力文档

文档评论(0)