- 在线时间
- 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>. j V& C) w3 l8 @1 P* D$ ]
< >[[/hide]</P>#include <iostream>5 f, l _" K9 r- G5 B" `! F
#include <fstream>/ s2 |! N2 z* t p" J$ W. a
using namespace std;
5 a) `1 Q; s/ C# R* j2 B4 I$ vifstream in("input.txt");5 {0 r0 `$ }" v
ofstream out("output.txt");
- N* w1 M0 S' J8 ?9 Fclass String* r |* o* n7 k9 j
{$ \8 L1 ^5 L, t
public:2 u: T" Z1 X2 J- @
String(char *s="");) l! m+ r7 D- _1 d6 B8 L" G; `& _
String(const String& s);
j8 A8 \8 w- J N+ X! [' H ~String();
{, O1 j4 `) M* s4 E0 x4 @ int Length() const;
0 p/ e7 s" d* Q8 u$ ?$ |5 m% q% p2 y int ReadString();
& f6 K7 ]. W8 t int Rs();" U8 R6 G( E; E% G! B( ]( I$ p
void Prefix();+ @: P+ }# q, \& d6 A/ ?( _
void ModifiedPrefix();
3 Y) a! y: O- [4 B% | int Match(int i,String& t);
" k* O: T/ @/ P" a. h' `2 K* P% G private:
& H9 B! q, R3 M+ |+ `& ^ r4 q0 g char *str;
' H' n4 c' a( c. ?" m' E; j7 [ int *pre;
( p7 t+ B1 Q! n int size;, X# C. K! y& a/ W' S. W3 j
};& p+ W# M( Y* p8 ^) c2 n0 K# o
String::String(char *s)
3 Q4 M4 } ^% P{
" d' e: t+ j) Q; v" h size=strlen(s)+1;2 s, A8 Z% d3 M2 y0 b7 t
str=new char[size];1 l ? i& o2 [' ?; Y4 g
if(str==0)# I, y& W3 Z/ U5 p
throw "error";
" P0 a* @' J1 ?9 x! \0 @; {: R strcpy(str,s);
, K6 {- {+ v7 O A3 @3 I pre=new int[size];
+ d1 y( a2 O! H9 B/ b1 E! p7 E if(pre==0)
& H- W0 B, |7 F$ A! \ throw "error";
; ~# x( K# @: ^! ^9 H# b- ]}& |7 t1 `- W3 u" @" u: y d; V; I l
String::String(const String& s)
, v0 |, V' X- I! b{
2 s8 `% w: j0 r! H' t5 J size=s.size;8 `* w; _" k3 ]# W/ `
str=new char [size];: e$ n0 t* W" R8 O2 z8 W
if(str==0): Y5 J" R6 c& i' v/ ]! r3 L+ o+ |
throw "error";3 |! c. n. v' Z( l# G
strcpy(str,s.str);
( L/ N3 y) [! Y pre=new int [size];
* A( J l$ n2 p3 N9 Y a% B8 @ if(pre==0)
q+ N; u* W* m) I7 s: S throw "error";
$ J/ q. F9 E1 y}; P7 }* i7 |" F S+ l- Q! I
String::~String()0 ?9 m& }* M- J" e; q O) Z* l* E
{; d' W. h+ `1 v% O0 o$ @- x \
delete [] str;
$ R5 M! k" b) A3 }" E* c8 K delete [] pre;
* |1 u t+ Y: _$ Q}
' Z# g1 q- ]$ H* D2 Dint String: ength() const
8 g9 F, G7 X4 c3 B- `{
$ M; d: ~) A7 H$ V5 K" j return size-1;
- z7 W$ G4 `5 v+ B( n3 Q}* q4 d7 K }8 S A9 F; f- f+ V
int String::ReadString()
+ Y. X9 l' ]: u0 x{5 Y/ k! d* o5 }/ K6 V
char tmp[256];
# G3 [; T0 @3 @9 W4 h if(in>>tmp)- f1 p* {; m1 q% U/ ]" @1 d n
{1 g9 V) T. Y- P+ W# c) j& A# k; x
delete [] str;7 V8 n1 b' @4 |* E* [$ ?# z
size=strlen(tmp)+1;
4 Q! G1 T: A K str=new char [size];
* M M/ d2 }; i! _6 e6 w strcpy(str,tmp);! V: s; b9 A1 Z* c5 ]# c6 o; ?% m
return size-1;# l& s }1 H( P) } V3 I4 `; _
}% @' K4 A4 J; m
else
7 |4 W4 K5 @) Q return -1;; X! h1 I1 J& }8 a
}% T- u; U- a) G+ R( i# m8 K2 H
int String::Rs()! A! ]1 }5 n' D( S5 O" z/ ]! u; |
{5 A3 H# X5 [$ k5 B/ K
char tmp[256],c;
- v, H! S+ Q6 ^! A int i=0;/ o- V& p j% t
while(in>>c)
8 h7 n2 v* ]9 a- V: B q4 H1 J6 X {4 k0 P0 E9 Z' t! K) h+ V
if(c!='*')! t/ ]" I4 ~/ f _, z$ U
tmp[i++]=c;
3 O1 z( g7 `- \1 _& r9 r3 a else if(c=='*'&&i>0)) @# A$ C. J7 G( {
break;
/ ]! N" h7 O" n }* ?2 ^2 b3 j- L0 ~, C1 o' L' h# B
if(i>0)
. Q8 {! J3 M/ ^' Y4 ^9 m {; q3 B0 n4 B6 \5 Y
delete [] str;
( |; B0 s/ X( R5 p size=i+1;2 h( F$ [0 T- d- o3 T; J
str=new char[size];2 A, g4 p, ~1 J$ |
if(str==0)
3 z1 J- L8 h) n- ^! t) T throw "error";" @3 q4 Q7 `! \0 `
for(i=0;i<size-1;i++)8 L( S% j+ @6 V. N% W8 Q
str=tmp;
, e7 F, ^. ^7 w* i$ u9 ]' y6 k return size-1;
+ I/ E( h- e, n# |; q# E6 L }
' }- D: C8 A, Q! x. x$ d/ A else
* P7 ]2 P6 K' h6 ?5 U8 {$ h( M% v return -1;
3 y9 I3 ?$ u3 ^- P}
! p# ?! ?3 ~5 O6 Bvoid String: refix()4 R k) s1 g. q" d
{
+ o5 P7 ^% D0 S7 ^# ` b int m=Length();: [! A1 F6 z8 x, ~
delete [] pre;: H0 x" }0 f% o3 G8 u
pre=new int [m+1];
. h$ D0 q0 O/ C0 a, R pre[1]=0;8 H) f7 Y3 d' Z9 i- T
int k=0;
% m0 ^5 v# c4 o5 w7 a* s for(int i=2;i<=m;i++)
5 e% M8 c C8 M3 v+ K {8 c9 ~+ F& v, {9 K) B: @* l
while((*(str+i-1)!=*(str+k))&&(k>0))
+ @# E6 N1 I7 b4 [ k=pre[k];
; Z. }& z" X- y8 O# u if(*(str+i-1)==*(str+k))& q; r4 H4 N. \- X9 }; E, F
pre=++k;/ t. [$ e! k d% @. w
else6 S9 \( ~% V# L$ n/ y$ [: J8 `
pre=0;
0 D" w! E8 l! z/ C7 l }
8 T, N2 W$ {; K, i. h, ~}
: s$ G& r2 a! @9 Nvoid String::ModifiedPrefix()- S! J: Y% {5 I, n- j" ^
{
% f$ Y0 i3 D# D! Q int *f;
1 A T% F4 D, a int m=Length();" X0 X, w0 f9 {; H
f=new int[m+1];# ]; [7 C8 C. a# M& r
Prefix();
! ~6 ~1 I! d: i' d3 ] for(int i=1;i<=m;i++)
& P8 V0 P7 n2 u f' B' T {
* ?) R5 O6 z7 `: h9 Y f=pre;
! ~" Y' ]5 u* Q }
4 `! c! X% Q4 f( E for(i=1;i<m;i++)2 `( o5 k Q$ K# [, r
{( j6 L& [% R5 k' C: w3 x8 Z1 q
int k=f;
, ?( @ N, ?& ]* ] z2 a if((k==0)||(*(str+i)!=*(str+k)))
- `2 q1 n9 b/ q2 e0 k& i6 N pre=k;
/ |9 [ b* E" A8 \5 V7 h else
" g; W) b3 O2 ~7 p, N pre=pre[k];
& |: ]9 \, J0 c; k7 r7 q }3 e. h; ]5 y" Z* W
delete []f;
* z- R& |' p* \. v; N5 Q}* g; G) ~" b/ C
int String::Match(int i,String& t)
5 D, P6 A( U/ O6 }. w# Z- p{
/ N+ p2 ~( ]3 S6 ] int j=0;
* g3 H; R) P3 ]+ d; b3 l5 d int n=Length(),m=t.Length();
( T. S7 S: _8 r+ A( F t.ModifiedPrefix();
" D8 a! g1 q. @" q- q4 P while((i<=n)&&(j<m))
% I8 a9 q2 @0 j0 Q8 V) {9 V6 N/ k) T if(str[i-1]==t.str[j])0 w, J1 O' ?" x n6 e) Q! o
{
3 Z- {/ `$ x9 r+ b. q! Z i++;, m1 {' x9 Z' C/ t7 b$ V
j++;/ W: T# U, a9 M+ n
}. r+ W1 l$ _ i" X) L) {
else % ]- N) C6 T1 B; S$ P
if(j==0)
9 r5 `" O1 U& t" h# p i++;$ e2 G$ K7 ~4 L; I
else ) y2 x0 d( ~- ^& z' b
j=t.pre[j];
+ a6 j0 j1 e! N R1 d q if(j<m)
3 t6 K( R5 T$ _8 S% K return 0;
2 B L) Z% }( x3 R( P9 Q else ! ?: X' t& }5 C* n
return i-1;
# }/ b" R7 s6 K9 X/ {/ k}
r. d3 h* Z! X; ]: X5 P6 kint main()
+ T# q! p7 u* i{8 Q b k9 |6 C- t+ o
if(in.fail())1 Z9 I- p6 t7 |1 n+ `6 ~
{
% s) M* t$ S( p0 V cout<<"the input.txt is not exist!";$ Z d0 B) T+ y
exit(1);}! G8 l. m% E2 T1 U+ p
int i=1;5 {0 j5 {$ E& C& d; {
String s,t;: V8 d$ R) |$ |
s.ReadString();* x( P2 Q3 p' V
while(t.Rs()+1)
3 {+ |& T- [9 }. Q: I {
4 ~" H: Q; L9 e5 g0 `2 g i=s.Match(i,t);4 P& ?& P7 ]: E# v& I& D. n" h
if(!i)9 c# `# e2 F( d% K1 F
{
- }, P# h( l8 D* | out<<"No"<<endl;4 z7 y4 U& Y$ ]# h/ v6 K2 A3 e9 L
break;$ d* l2 l- B' @) F2 Y/ N
}+ _8 N$ G3 ?% [
}
2 d! q2 e8 U; z/ ?9 g7 S if(i)
5 u& u$ r2 Y! j# f out<<"Yes"<<endl;
3 [- y0 u$ ~, l) Y% \, F return 0;
* C9 t9 [3 i" d9 W; ~: @/ \} |
zan
|