- 在线时间
- 0 小时
- 最后登录
- 2005-4-22
- 注册时间
- 2005-4-22
- 听众数
- 2
- 收听数
- 0
- 能力
- 0 分
- 体力
- 72 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 26
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 7
- 主题
- 2
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   22.11% 该用户从未签到
 |
< >很经典的一道问题,我想大家应该都会,我这次只是想帮助那些还不是很懂得人,还是一样附上解题报告和源代码。觉得好的就顶,主要是用模式串来解决。</P>& g' s0 {* G. H( L9 m3 E) ]1 N
< >[[/hide]</P>#include <iostream>
- B L0 I: ^2 o+ F# P+ v( F6 A#include <fstream>% F8 @6 ]; l; R5 \
using namespace std;
. {" C1 Y: L9 _/ Z; gifstream in("input.txt");
( [+ G* @% D; g; a/ Fofstream out("output.txt");. b7 U+ E5 p1 j5 m L0 Z
class String5 f# C5 K5 U4 c) O/ D
{ I% e1 C: H- Z$ l& e
public:
- b2 A3 a7 A8 G1 |, V String(char *s="");
% N$ P0 f3 x4 A7 @5 w( \# H String(const String& s);) m- m" o& p* q1 H% X
~String();6 z1 U$ M" Y$ a6 H; j
int Length() const;9 N- p% X: c+ Y) O
int ReadString();
0 s0 K } w3 O! J- s/ _8 A- U! o int Rs();
" k& G/ X" [' w1 S, [, b void Prefix();
( w. i( F$ l$ q void ModifiedPrefix();
* h M) u% ?4 I! e8 x8 |4 x; N3 ` int Match(int i,String& t);5 f+ s3 @1 L6 N( u( c
private:0 w4 F& ~) [& p3 [
char *str;* \1 g9 ?7 K. h# N/ L. A
int *pre;5 w7 a* @ a$ [8 _
int size;1 g0 V4 _, ?" Q: K+ w. X9 {
};
- J" M( B( `1 O, WString::String(char *s)
2 E# X* p+ H: F( W2 Y: E! ? n{
7 \/ B6 M3 ^; t- ?* Y4 G size=strlen(s)+1;# ~3 d3 R' A4 t1 L- O j W
str=new char[size];
# ~+ _0 o( S. Y6 r) d& Z( m. V if(str==0)
7 M+ |/ I/ ]3 o& h* ]( G; p ] throw "error";
) x) x% K' p! s& s strcpy(str,s);
. I0 Y9 z7 K3 i, b9 L5 ^; M* u pre=new int[size];7 K, ]8 C9 |- I0 o# U6 O$ U0 p/ h
if(pre==0)' W0 M+ J% D: r. ?; f
throw "error";
9 {; [. r# C4 }/ r( @' J}' a$ X/ W( d4 e, ]
String::String(const String& s)
; M6 D# F' X |( s+ l: Y{
! j) b! K+ \2 H. m4 L& B" ` size=s.size;/ p- m3 g6 V7 `
str=new char [size];
7 R3 q1 E5 u/ w if(str==0)
, y/ h* ~. |6 t& |* Z9 z throw "error";
- J9 k8 P7 E& w9 d+ F1 b! k strcpy(str,s.str);
$ \1 P( k5 O" q& A, G% c3 Q) q( Y" Y pre=new int [size];
1 |( e B# ^$ V& ~& S8 |: J if(pre==0)
2 ~& F# ~0 C# b) p throw "error";4 C9 ?3 I9 y: o: E) a
}* \. H" ^" N" \' K2 A; M5 o2 O
String::~String()
1 X. c* z8 q6 ]0 o, q{
8 Q; ]/ \7 }3 q8 a& p0 z delete [] str;/ V) f$ C0 o$ G, K
delete [] pre;
. c9 @7 T# w3 X}
' ^& @$ ]; x+ i$ ]int String: ength() const
0 \/ j5 T: ?8 m& n' O- Z3 ~{
9 P/ }: N! H& t( f' C, e return size-1;
}" X/ c/ m# k5 H* F8 o}4 @1 j1 N5 F8 T4 D1 g3 H9 I: E
int String::ReadString()
J6 x% K8 A7 I7 ?7 ~! A{
( I" y$ y) [8 v7 y$ u& {4 m" W, m char tmp[256];! b4 e- l9 b3 E8 i6 T# v: t6 s
if(in>>tmp)
4 b9 Q% o. D6 j3 v$ f {
! a6 l1 Y! }% S9 j2 b" u delete [] str;
. S. _: {8 W9 G, C" _7 z size=strlen(tmp)+1;1 z) D( d8 U( A; o: K
str=new char [size];
7 F; ~* q6 T% q6 Q- v8 o) n0 m- T strcpy(str,tmp);$ R4 n t( b4 M7 `# r% D- ]
return size-1;' T5 G' B, n: W$ j( t
}& ^8 c! R4 H$ w# g9 U7 G, h
else
: h6 t& w; P! k* d) E2 q. f* H return -1;
4 j8 j' z B2 x9 p, K# y- I! n}
C9 ?9 C+ n6 w; x* F" Qint String::Rs()9 S! s/ ~. Z( g9 l' o# ?: j
{. @1 y+ W: N, D- X
char tmp[256],c;% h% E0 r( }/ K/ s6 R1 X2 g
int i=0;
" O$ F) Y) o, _! v/ a( r9 \ while(in>>c)) [4 [. r. K' u5 P. k' B
{0 g3 b; N/ H2 W0 w, d: l
if(c!='*')
% y) E) F' o5 o tmp[i++]=c;
+ v) G: b- R0 m% l else if(c=='*'&&i>0)
! L4 x1 @% E5 F! f1 @! w break;# U) W, t+ V* P% q. ]
}
6 h% K1 O; `9 Z3 J if(i>0) {7 B# j5 B) b. N$ B9 K4 M$ O
{
+ A/ S- M* L6 L0 }2 H6 s, r" Q delete [] str;- n1 E( S& j" Y- W8 }
size=i+1;* m+ Q( m$ X: t: M7 Z% ?5 r
str=new char[size];: p' F0 a* e! x: j, K# d
if(str==0)1 _. l2 P* ^' L& V
throw "error";
; t" g% Y8 C4 v- H7 z for(i=0;i<size-1;i++). s" c2 x8 k$ P: }
str=tmp; % {" t' G+ g4 e; ?1 K. s
return size-1;
1 h3 x+ Z$ P- m7 V" q }
3 y2 P7 o% u7 @- s: S4 w: f9 Q else
+ c/ o/ P( ~' x- f return -1;3 B- S2 {% I# o# Y
}
& l( z' V9 ^; s: Y" M* M6 ?void String: refix()
4 i& T$ ]( p! [! q{
( ?7 v/ t1 ]* G" U- v int m=Length();
1 l6 V0 j" y0 l# y; Q delete [] pre;
, G- L$ }$ ^* x- M* ~, x pre=new int [m+1];
4 I6 x1 r5 X3 i4 ~) Q pre[1]=0;
" d$ s- @4 }$ G$ m5 B int k=0;/ A/ Z8 S# |# S; t- b
for(int i=2;i<=m;i++)
p1 |7 y3 d8 L. g {
+ M; K7 D. l M( K while((*(str+i-1)!=*(str+k))&&(k>0))% {& `- s7 J, f' Y
k=pre[k];# e" D" |" t% g" N
if(*(str+i-1)==*(str+k))8 u: Y8 e& s7 ]& ^. O
pre=++k;
: w" b' v. ~- o$ V2 \) m else
* i: @" i, a4 E% R7 ?) n" Y/ U6 g pre=0;" q3 s4 i I! m/ v0 W& E0 q
}
% b4 _4 j) E0 D; f c8 w: Q}
: U( w0 I& j! p# a) P& ?8 \void String::ModifiedPrefix()4 D. N* W8 h, X( F7 d/ [8 I
{/ t0 o9 c1 y9 W5 t% K1 n ?
int *f;
( I+ D% F$ j2 }! `7 } int m=Length();
0 b5 `0 k( v4 K; {* P f=new int[m+1];0 {+ O1 \3 e% @/ ]2 ]* E/ q" G
Prefix();8 f! |; c: K. ?$ x
for(int i=1;i<=m;i++)
$ n' X3 ]8 I- k. U1 E! q {6 ^6 I9 ]: ~1 J# a9 w7 I# X
f=pre;
6 y" i3 }; }8 {3 \( |/ [ }
, {0 }" H0 R E4 c* K3 l/ f( A for(i=1;i<m;i++)
$ C- y2 F: Z# B$ C1 { {6 T' N0 R- b. n0 e% \% b
int k=f;
[$ ~8 @3 D! r4 V E' v if((k==0)||(*(str+i)!=*(str+k)))
/ i0 d0 h) W9 u% s$ P pre=k;4 C* l9 Q+ L7 c$ @: Q6 w, \
else
: p. i! d2 F) ^+ }; E* c, g pre=pre[k];3 \" E4 Y: U% B' h; ?3 U
}9 B E! E( q9 K" D8 F# K/ ]# z
delete []f; e/ K% z6 g# F/ z* k- h
}* P+ F# L4 M8 N9 V$ Q& L# a
int String::Match(int i,String& t)" G" h1 w. w4 ^5 u
{+ W+ j- h" T1 p3 m8 Y1 |& P
int j=0;) ?/ T' s6 ^! s: c. `
int n=Length(),m=t.Length();
% J M( V' o3 d# d6 R t.ModifiedPrefix();" W8 p8 ~6 a5 a
while((i<=n)&&(j<m))5 J+ s0 L1 a1 x* ]0 h/ A
if(str[i-1]==t.str[j])/ h9 L6 J1 z" v. w5 `# d
{
, b- x# Q2 P$ Q6 j5 H) x; i i++;
7 Z7 v$ f* D6 [% l/ v5 I! d j++;
! K3 V* k; q' \ R8 @2 u }( n; s6 V8 W) a* @0 F6 N$ S
else ! u( T n g$ `
if(j==0)
2 r4 k& |) F9 P! H- s1 m: _ i++;
5 w0 }* x2 ` ?4 k, L0 A7 X" ^9 B else
( y$ D q; ? M j=t.pre[j];! y9 v* f; X: ]+ J' j
if(j<m)
: S4 |4 H) V: _7 B! K6 }9 q: R return 0;; [% O) `' }5 E$ ?4 y. b
else
( c9 J" |! B9 R( p U return i-1;
+ u$ i9 U V7 h7 \* C}
" E% }. p9 f) X: z [2 Pint main()8 m4 V1 @7 {; b% d7 w- Q
{
& T# p: h1 H! ? if(in.fail())
4 K2 |% w5 G! `% I6 H; c! m# ~9 z3 d {3 p3 ]9 R2 j) Y" z: ?0 B
cout<<"the input.txt is not exist!";6 k. q# B$ V5 M3 I
exit(1);}
. M- E5 z' I# B: j. d int i=1;
5 V" x+ h+ v) j% ^* ~! I String s,t;/ ?6 I0 Z- B3 n
s.ReadString();* F7 U( L" k0 R% C- C% V
while(t.Rs()+1)
) L. m0 q1 j* \$ S' I {
% F O$ T8 J( f$ I/ ` i=s.Match(i,t);
" y! j: J3 W' H! Y if(!i)
4 k% ~6 n4 Q; d0 z$ \. h" y- l {
4 l7 X; n7 P% @5 i out<<"No"<<endl;( J5 c. e- a8 y3 Q: t
break;* H3 ^( F$ I' `/ {5 p. X
}0 _4 i4 @) b! v- j% [
}( s ^* G* R6 x) h1 \0 e: c
if(i)* v+ y4 o' x0 M& i `4 d5 ?' m
out<<"Yes"<<endl;
2 q. D8 O) o s. [! k- ?% f return 0;
- x/ |+ r# z- p4 f i} |
zan
|