Pagini recente » Cod sursa (job #2710881) | Cod sursa (job #780273) | Cod sursa (job #2131347) | Cod sursa (job #2959787) | Cod sursa (job #754415)
Cod sursa(job #754415)
#include<string>
#include<vector>
#include<fstream>
#include<algorithm>
using namespace std;
#define NMAX 20000200
int PI[NMAX];
void compute_pie( string& P )
{
int sz = P.size();
PI[0] = 0;
int i , k = 0;
for( i = 1; i < sz; ++i )
{
while( k > 0 && P[k] != P[i] )
k = PI[k];
if( P[k] == P[i] )
++k;
PI[i] = k;
}
}
int main()
{
fstream f ("strmatch.in", fstream::in );
fstream g ("strmatch.out", fstream::out );
//freopen("kmp.in","r",stdin);
//freopen("kmp.out","w",stdout);
string P, S;
getline(f,P);
getline(f,S);
int sz = S.size(), p_sz = P.size();
P.resize( p_sz + 1 );
compute_pie( P );
int ans[1024], ans_count = 0;
int i, matched = 0;
for( i = 0; i < S.size(); ++i )
{
while( matched > 0 && P[ matched ] != S[i] )
matched = PI[ matched -1 ];
if( P[ matched ] == S[i] )
++matched;
if( matched == p_sz )
{
if( ans_count < 1001 )
ans[ ans_count ] = i - p_sz + 1;
++ans_count;
}
}
g << ans_count << "\n";
sz = min( ans_count, 1000 );
for( i = 0; i < sz; ++i )
g << ans[ i ] << " ";
g << "\n";
return 0;
}