Pagini recente » Istoria paginii runda/b0ss_d3_b0ss_7/clasament | Monitorul de evaluare | Profil Ovidiu3000 | Profil M@2Te4i | Cod sursa (job #2274744)
#include <fstream>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
const int N = (int)2e6 + 5;
int solutions[N];
int cnt = 0;
string pat;
string s;
int m, n;
int Lps[N];
inline void BuildLps()
{
int i = 1;
int len = 0;
Lps[0] = 0;
while(i < m)
{
if(pat[i] == pat[len])
{
len++;
Lps[i] = len;
i++;
}
else
{
if(len == 0)
{
i++;
}
else
{
len = Lps[len - 1];
}
}
}
}
inline void KMP()
{
int i = 0;
int j = 0;
while(i < n)
{
if(s[i] == pat[j])
{
i++;
j++;
}
if(j == m)
{
solutions[++cnt] = i - m;
j = Lps[j - 1];
}
else
{
if(i < n && s[i] != pat[j])
{
if(j == 0)
{
i++;
}
else
{
j = Lps[j - 1];
}
}
}
}
}
int main()
{
fin >> pat >> s;
m = pat.size();
n = s.size();
BuildLps();
KMP();
fout << cnt << "\n";
if(cnt > 1000)
{
cnt = 1000;
}
for(int i = 1; i <= cnt; i++)
{
fout << solutions[i] << " ";
}
fout << "\n";
return 0;
}