求救!! 急待!!!

hdx123 2004-11-09 09:22:54
题目: 请用visual c++编写一个程序,用于实现KMP算法(模式匹配算法),要求分别编写kmp比较函数和get_next或get_nextval函数。
主函数中完成对主串和模式串的输入,并调用以上两个函数进行匹配,输出模式串在主串中的位置(如果匹配失败输出相应信息)。
...全文
47 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzheng318 2004-11-09
  • 打赏
  • 举报
回复
//.h
#ifndef _MY_STRING_H_
#define _MY_STRING_H_

#include <iostream.h>

/*
* The next line is used because Codewarrior has a conflict with
* the STL string. Make sure to put the #include of this file
* AFTER all the system includes.
*/
#define string String

class StringIndexOutOfBounds { };

class string
{
public:
string( const char *cstring = "" ); // Constructor
string( const string & str ); // Copy constructor
~string( ) // Destructor
{ delete [ ] buffer; }

const string & operator= ( const string & rhs ); // Copy
const string & operator+=( const string & rhs ); // Append

const char *c_str( ) const // Return C-style string
{ return buffer; }
int length( ) const // Return string length
{ return strLength; }

char operator[]( int k ) const; // Accessor operator[]
char & operator[]( int k ); // Mutator operator[]

enum { MAX_LENGTH = 1024 }; // Maximum length for input string
private:
char *buffer; // storage for characters
int strLength; // length of string (# of characters)
int bufferLength; // capacity of buffer
};

ostream & operator<<( ostream & out, const string & str ); // Output
istream & operator>>( istream & in, string & str ); // Input
istream & getline( istream & in, string & str ); // Read line

bool operator==( const string & lhs, const string & rhs ); // Compare ==
bool operator!=( const string & lhs, const string & rhs ); // Compare !=
bool operator< ( const string & lhs, const string & rhs ); // Compare <
bool operator<=( const string & lhs, const string & rhs ); // Compare <=
bool operator> ( const string & lhs, const string & rhs ); // Compare >
bool operator>=( const string & lhs, const string & rhs ); // Compare >=

#endif
//.cpp
#include <string.h>
#include "mystring.h"

string::string( const char * cstring )
{
if( cstring == NULL )
cstring = "";
strLength = strlen( cstring );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, cstring );
}

string::string( const string & str )
{
strLength = str.length( );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer,str.buffer );
}

const string & string::operator=( const string & rhs )
{
if( this != &rhs )
{
if( bufferLength < rhs.length( ) + 1 )
{
delete [ ] buffer;
bufferLength = rhs.length( ) + 1;
buffer = new char[ bufferLength ];
}
strLength = rhs.length( );
strcpy( buffer, rhs.buffer );
}
return *this;
}

const string & string::operator+=( const string & rhs )
{
if( this == &rhs )
{
string copy( rhs );
return *this += copy;
}

int newLength = length( ) + rhs.length( );
if( newLength >= bufferLength )
{
bufferLength = 2 * ( newLength + 1 );

char *oldBuffer = buffer;
buffer = new char[ bufferLength ];
strcpy( buffer, oldBuffer );
delete [ ] oldBuffer;
}

strcpy( buffer + length( ), rhs.buffer );
strLength = newLength;
return *this;
}

char & string::operator[ ]( int k )
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}

char string::operator[ ]( int k ) const
{
if( k < 0 || k >= strLength )
throw StringIndexOutOfBounds( );
return buffer[ k ];
}

ostream & operator<<( ostream & out, const string & str )
{
return out << str.c_str();
}

istream & operator>>( istream & in, string & str )
{
char buf[ string::MAX_LENGTH + 1 ];
in >> buf;
str = buf;
return in;
}

istream & getline( istream & in, string & str )
{
char buf[ string::MAX_LENGTH + 1 ];
in.getline( buf, string::MAX_LENGTH );
str = buf;
return in;
}

bool operator==( const string & lhs, const string & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) == 0;
}

bool operator!=( const string & lhs, const string & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) != 0;
}

bool operator<( const string & lhs, const string & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) < 0;
}

bool operator<=( const string & lhs, const string & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) <= 0;
}

bool operator>( const string & lhs, const string & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) > 0;
}

bool operator>=( const string & lhs, const string & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) >= 0;
}
xunfengxxx 2004-11-09
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

using namespace std;

void
Getnext( const string str, int next[] )
{
int i, j;
next[ 0 ] =next[1]= 0;

j = 0;
i = 1;

while ( i<str.size() )
{
if ( j == 0 )
{
++i;
if ( str[ 0 ]==str[ i-1 ] ) ++j;
next[ i ] = j;
}
else if ( str[ i ]==str[ j ] )
{
++i;
++j;
next[ i ] = j;
}
else
{
j = next[j];
}

}

for ( i=0; i<str.size(); ++i )
{
cout << "\tnext[ " << i << " ] = ";
cout << next[ i ] << endl;
}
}

bool
Kmp( string s, string t, int next[] )
{
int i(0), j(0);
int slen = s.size();
int tlen = t.size();

while ( i<slen && j<tlen )
{
if ( s[ i ]==t[ j ] )
{
++i;
++j;
}
else if ( j == 0 )
{
++i;
}
else
{
j = next[ j ];
}
}

if ( j == tlen ) return true;
else return false;
}

int
main()
{
string s, t;
cout << "\t请输入主串: ";
cin >> s;
cout << "\t模式串为: ";
cin >> t;

int *next = new int[ t.size() ];

Getnext( t, next );
if ( Kmp( s, t, next ) )
{
cout << "\t\t匹配" << endl;
}
else
{
cout << "\t\t不匹配" << endl;
}

return 0;

}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧