Pagini recente » Cod sursa (job #1133156) | Cod sursa (job #2499176) | Istoria paginii runda/wrsff/clasament | Istoria paginii runda/ursus_polar_de_munte/clasament | Cod sursa (job #1384364)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
static const int DIM = 2000000;
int T[DIM];
std::string first;
std::string second;
std::vector<int> poz;
void kmp_search()
{
int m = 0;
int i = 0;
while ( m + i < second.size() )
{
if ( first[i] == second[i+m] )
{
if ( i == first.size() - 1 )
{
poz.push_back(m);
i = -1;
m++;
}
++i;
}
else
{
if ( T[i] > -1 )
{
m = m+i-T[i];
i = T[i];
}
else
{
i = 0;
++m;
}
}
}
}
void kmp_table()
{
T[0] = -1;
T[1] = 0;
int pos = 2;
int cnd = 0;
int size = second.size();
while( pos < size )
{
if ( second[pos-1] == second[cnd] )
{
++cnd;
T[pos] = cnd;
++pos;
}
else
{
if ( cnd > 0 )
{
cnd = T[cnd];
}
else
{
T[pos] = 0;
++pos;
}
}
}
}
int main( int argc, char* argv[] )
{
ifstream inputFile("strmatch.in");
ofstream outputFile("strmatch.out");
getline(inputFile,first);
getline(inputFile,second);
kmp_table();
kmp_search();
outputFile << poz.size() << std::endl;
int dim = poz.size() > 1000 ? 1000 : poz.size();
for ( int i = 0; i < poz.size(); ++i )
{
outputFile << poz[i] << " ";
}
inputFile.close();
outputFile.close();
return 0;
}