BF算法
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
#define fori(i, a, b) for (int i = (a); i < (b); i++)
#define ford(i, b, a) for (int i = (b) - 1; i >= a; i--)
#define trav(a, arr) for (auto& a : arr)
#define all(x) (x).begin(), (x).end()
const int maxn = 5e3 + 5 ;
int index ( char * p, char * m, int len, int len2)
{
int i, i2;
for ( i = 0 ; i < len; i++ )
{
for ( i2 = 0 ; i2 < len2; i2++ )
{
if ( p[ i + i2] != m[ i2] ) break ;
}
if ( i2 == len2)
{
return i;
}
}
return - 1 ;
}
int main ( void )
{
char ch;
char p[ maxn] , m[ maxn] ;
int len, len2;
len = len2 = 0 ;
while ( ( ch = getchar ( ) ) != '\n' ) p[ len++ ] = ch;
while ( ( ch = getchar ( ) ) != '\n' ) m[ len2++ ] = ch;
printf ( "%d" , index ( p, m, len, len2) ) ;
}
KMP
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define fori(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const int maxn = 1e5 + 5 ;
int m_next[ maxn] ;
char p_str[ maxn] , m_str[ maxn] ;
void getnext ( int len)
{
int i = 0 , i2 = - 1 ;
m_next[ 0 ] = - 1 ;
while ( i < len)
{
if ( i2 == - 1 || m_str[ i] == m_str[ i2] )
{
i++ , i2++ ;
m_next[ i] = i2;
}
else i2 = m_next[ i2] ;
}
}
int get_index ( int len, int len2)
{
int i = 0 , i2 = 0 ;
while ( i < len && i2 < len2)
{
if ( p_str[ i] == m_str[ i2] || i2 == 0 ) i++ , i2++ ;
else i2 = m_next[ i2] ;
}
if ( i2 >= len2) return i - len2;
else return - 1 ;
}
int main ( void )
{
int len = 0 , len2 = 0 , ans;
char ch;
printf ( "请在第一行输入主串,第二行输入模式串:\n" ) ;
while ( ( ch = getchar ( ) ) != '\n' ) p_str[ len++ ] = ch;
while ( ( ch = getchar ( ) ) != '\n' ) m_str[ len2++ ] = ch;
getnext ( len2) ;
ans = get_index ( len, len2) ;
ans == - 1 ? printf ( "匹配失败!" ) : printf ( "对应位置为(从0开始):%d" , ans) ;
return 0 ;
}
KMP优化
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define fori(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const int maxn = 1e5 + 5 ;
int m_next[ maxn] ;
char p_str[ maxn] , m_str[ maxn] ;
void getnext ( int len)
{
int i = 0 , i2 = - 1 ;
m_next[ 0 ] = - 1 ;
while ( i < len)
{
if ( i2 == - 1 || m_str[ i] == m_str[ i2] )
{
i++ , i2++ ;
m_next[ i] = i2;
}
else i2 = m_next[ i2] ;
}
}
void getnextplus ( int len)
{
fori ( i, 0 , len)
{
while ( m_next[ i] != - 1 && m_str[ i] == m_str[ m_next[ i] ] ) m_next[ i] = m_next[ m_next[ i] ] ;
}
}
int get_index ( int len, int len2)
{
int i = 0 , i2 = 0 ;
while ( i < len && i2 < len2)
{
if ( p_str[ i] == m_str[ i2] || i2 == 0 ) i++ , i2++ ;
else i2 = m_next[ i2] ;
}
if ( i2 >= len2) return i - len2;
else return - 1 ;
}
int main ( void )
{
int len = 0 , len2 = 0 , ans;
char ch;
printf ( "请在第一行输入主串,第二行输入模式串:\n" ) ;
while ( ( ch = getchar ( ) ) != '\n' ) p_str[ len++ ] = ch;
while ( ( ch = getchar ( ) ) != '\n' ) m_str[ len2++ ] = ch;
getnext ( len2) ;
getnextplus ( len2) ;
ans = get_index ( len, len2) ;
fori ( i, 0 , len2)
printf ( "%d " , m_next[ i] ) ;
ans == - 1 ? printf ( "匹配失败!" ) : printf ( "对应位置为(从0开始):%d" , ans) ;
return 0 ;
}