??? (String) - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

??? (String)

Description:

2004.2-5 (String) n ( 0 ) S : c1c2c3 cn S ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 46
Provided by: Jame107
Category:
Tags: string

less

Transcript and Presenter's Notes

Title: ??? (String)


1
???????
  • 2004.2-5

2
??? (String)
  • ????n ( ? 0 ) ????????,?? S c1c2c3cn
  • ??,S????
  • c1c2c3cn???
  • ci?????
  • n??????

3
?????????????
const int maxLen 128 class String
int curLen //?????? char ch
//?????? public String ( const String
ob) String ( const char init )
String ( ) String ( ) delete ch
int Length ( ) const return curLen
4
String operator ( ) ( int pos, int len )
int operator ( const String ob )
const return strcmp (ch, ob.ch) 0 int
operator ! ( const String ob ) const
return strcmp (ch, ob.ch) ! 0 int
operator ! ( ) const return curLen
0 String operator ( const String ob
) String operator ( const String ob )
char operator ( int i ) int Find (
String pat ) const
5
??????????
StringString ( const String ob )
//??????????ob?? ch new charmaxLen1
if ( !ch ) cout ltlt Allocation
Error\n exit(1)
curLen ob.curLen strcpy ( ch, ob.ch )

6
StringString ( const char init ) //??????
???????init?? ch new charmaxLen1
if ( !ch ) cout ltlt Allocation
Error\n exit(1) curLen
strlen (init) strcpy ( ch, init
)
7
StringString ( ) //?????????? ch new
charmaxLen1 if ( !ch ) cout
ltlt Allocation Error\n exit(1)
curLen 0 ch0 \0
8
?????????
poslen -1 poslen -1 ? curLen-1
? curLen
9
String String operator ( ) ( int pos, int
len ) //????pos????????len??? //??????
if ( pos lt 0 poslen -1 gt maxLen
len lt 0 ) temp.curLen 0
//???? temp.ch0 '\0'
else //????
String temp new String //????
10
if ( poslen -1 gt curLen )
len curLen - pos temp?curLen len
//???? for ( int i0, jpos iltlen i, j
) temp?chi chj //?????
temp?chlen \0 //????
return temp
11
String String operator ( const String ob )
//???????ob?? if ( ob ! this )
delete ch ch
new char maxLen1 //???? if ( ! ch
) cerr ltlt Out Of Memory!\n exit
(1) curLen ob.curLen
//??? strcpy ( ch, ob.ch )
12
else cout ltlt Attempted assignment of
a String to itself!\n
return this char Stringoperator (
int i ) //????????i??? if ( i lt 0 i gt
curLen ) cout ltlt Out Of Boundary!\n
exit (1) return chi
13
String String //??? operator ( const
String ob ) char temp ch
//?????? curLen ob.curLen
//????? ch new char maxLen1 if (
! ch ) cerr ltlt Out Of Memory!\n
exit (1) strcpy ( ch, temp
) //?????? strcat ( ch, ob.ch
) //??ob??? delete temp return
this
14
??????
  • ?? ???????(?????)??????
  • ?? ??????,??????,??????
  • ?? ?? T Beijing
  • ?? P jin
  • ???? 3

15
?1? T a b b a b a ????? P a b a
???? ?2? T a b b a b a P a b
a ?3? T a b b a b a P a b
a ?4? T a b b a b a P
a b a ?
16
int StringFind ( String pat ) const
//??????? char p pat.ch, s ch
int i 0 if ( p s ) //???????
while ( i lt curLen - pat.curLen ) if (
p s ) //????? if ( !p )
return i //?? else i s chi
p pat.ch
//???????,????? //????,???? return -1

17
???????
?? T t0 t1 t2 tm-1 tn-1 ? ? ?
? ?? pat p0 p1 p2 pm-1 ??
T t0 t1 t2 tm-1 tm tn-1 ? ?
? ? ?? pat p0 p1 pm-2
pm-1 ?? T t0 t1 ti ti1 tim-2
tim-1 tn-1 ? ? ? ?
?? pat p0 p1 pm-2 pm-1
18
  • ?????????????
  • ??????n-m1?,????m?,
  • ??????(n-m1)m
  • ???????????,?????
  • ?????????????????
  • ???????????????
  • ???????(KMP)???????
  • ?????????,??n-m1?,????????(n-m)m n
  • ????m????,?????????? n

19
T t0 t1 ts-1 ts ts1 ts2 tsj-1
tsj tsj1 tn-1 ?
? ? ? ? ? P p0
p1 p2 pj-1 pj pj1 ?? ts
ts1 ts2 tsj p0 p1 p2 pj (1)
???? P ??? T ??,???? p0 p1 p2 pj-1 pm-1
ts1 ts2 ts3 tsj tsm ?? p0 p1
pj-1 ? p1 p2 pj (2) ???????
p0 p1 pj-1 ? ts1 ts2 tsj ???????
20
??,? p0 p1 pj-2 ? p2 p3 pj ?????????,???
p0 p1 pj-2 ? ts2 ts3 tsj ???????k?,??
p0 p1 pk1 ? pj-k-1 pj-k pj
? p0 p1 pk pj-k pj-k1
pj ? p0 p1 pk tsj-k tsj-k1
tsj ? ?
? pj-k pj-k1 pj
21
k ????? ??????? j ??????, k ??????? j
?????,?????? ?????? f (j)???? ?????? f (j)
????? ?? j 0,?????? 1,?????? p0? ?? j gt
0,???????,?????? pf(j-1)1?
22
?? ?? P p0 p1pm-2 pm-1
?? ?????? f (j)
23
?????? (Naive)
int index_naive(char S , charT )
i j 0 while (i lt S_len j lt T_len)
if (S i T j ) i j
else i i - (j - 1) j 0
if (j T_len) return (i - T_len) else
return -1
24
??????????
T
S
????????? -lt
25
???????
T
S
???????? -)
26
Knuth-Morris-Pratt??
Demo
k 2 next 5
i 7
a
b
c
a
a
a
b
a
a
b
c
a
c
a
b
a
a
b
c
j 5
??? S i ??? T j ???,i ???,? j
???????????? k??? KPM ???????????? k next j
????
27
KMP ????(I)
Si - k ... i -1 T0 ... k -1
i
S
a
b
c
a
a
a
b
a
a
b
c
a
c
a
b
a
a
b
c
T
j
k
28
KMP ????(II)
Si - k ... i -1 T j - k ... j -1
i
S
a
b
c
a
a
a
b
a
a
b
c
a
c
a
b
a
a
b
c
T
j
k
29
KMP ????(III)
Si - k ... i -1 T0 ... next j -1
i
S
a
b
c
a
a
a
b
a
a
b
c
a
c
a
b
a
a
b
c
T
k
next j
30
KMP ????(IV)
? (I) ,(II),? (III) ????
T0 ... k -1 T j - k ... j -1 T0 ...
next j -1
???? k next j ???(??????)
??????? next j ??? S ???
31
next j ????
????? i ??
32
KMP??
int index_kmp(char S , charT ) i
j 0 while (i lt S_len j lt T_len)
if (j -1 S i T j ) i
j else j next j
if (j T_len) return (i - T_len) else
return -1
Naive
33
next j ?????
  • ???? next0 -1
  • ? nextj k,? nextj1
  • ? Tj Tk,? nextj1 k 1 nextj 1
  • ??(Tj ? Tk),
  • ?Tj Tnextk, ? nextj1 nextk 1
  • ??......

j5
a
b
a
a
b
c
T
a
c
next6 next51 nextnextnext5 1
nextnext2 1 next0 1 -1 1 0
j
0
1
2
3
4
5
6
7
-1
0
0
1
1
2
0
1
next j
34
next j ??
void get_next(char T , int next)
i 0 j -1 next0 -1 while (i lt
T_len) if (j -1 T i T j
) i j next i j
else j next i
35
??KMP??????? ?1? ?? a c a b a a b a a b c a c
a a b c ?? a b a a b c a c ? j 1
? j f (j-1)1 0 ?2? ?? a c a b a a b a a b
c a c a a b c ?? a b a a b c a c
?
j 5 ? j f (j-1)1 2 ?3? ?? a c a b a a b
a a b c a c a a b c ?? (a b) a a b
c a c ?
36
int String fastFind ( String pat ) const
//??????KMP???? int posP 0, posT
0 int lengthP pat.curLen, lengthT
curLen while ( posP lt lengthP posT lt
lengthT ) if ( pat.chposP
chposT ) posP posT
//?????? else if ( posP
0 ) posT //??? else
posP pat.f posP-11 if ( posP lt lengthP
) return -1 else return posT -
lengthP
37
?????? f j ??? ????f 0 -1,???f j?f
j1? ??, f (1) j f j , f
(m) j f f (m -1) j
38
f 0 -1 j 1?, f 01 0, p0 ? p1, f 1
-1 j 2?, f 11 0, p0 p2, f 2 f
11 0 j 3?, f 21 1, p1 ? p3,
f 11 0, p0 p3, f 3 f 11 0 j
4?, f 31 1, p1 p4, f 4 f 31 1
39
void Stringfail ( ) //?????? int
lengthP curLen f 0 -1
//???? for ( int j1 jltlengthP j )
//???f j int i f j-1 while (
(chj) ! (chi1) i gt 0 ) i
f i //?? if ( (chj)
(chi1) ) f j i1 else f j
-1
40
?????????
  • ????
  • ??????

41
????
  • Microsoft NotePad
  • Microsoft Word (WYSIWYG)
  • Unixs VI
  • Emacs and Emacsen who use
  • GNU Emacs
  • XEmacs

42
????????????
i
text i
define MAXLINE 65535 typedef struct char
line int length Line Line textMAXLINE
100
101
102
103
104
105
43
??????
  • ??????????????

??????
????
? ? ?
????
?? ??
???
????
44
??????(??????????)
??????
????
45
???????????
define MaxKeyNum 2500 typedef struct
HString key LinkList bnolist
IdxTermType typedef struct IdxTermType
itemMaxKeyNum1 int last IdxListType
Write a Comment
User Comments (0)
About PowerShow.com