Pagini recente » Cod sursa (job #2081241) | Cod sursa (job #1081132) | Cod sursa (job #1236371) | Cod sursa (job #1090398) | Cod sursa (job #3123135)
#include <fstream>
using namespace std;
ifstream in;
ofstream out;
int np, p[1005], T[2000005];
int main()
{
in.open("strmatch.in");
out.open("strmatch.out");
string s, w;
in >> w >> s;
int j = 0, k = 0;
int pos = 1; //(the current position we are computing in T)
int cnd = 0; //(the zero-based index in W of the next character of the current candidate substring)
T[0] = -1;
while(pos < w.size())
{
if(w[pos] == w[cnd])
T[pos] = T[cnd];
else
{
T[pos] = cnd;
while(cnd >= 0 && w[pos] != w[cnd])
cnd = T[cnd];
}
pos = pos + 1; cnd = cnd + 1;
}
T[pos] = cnd; //(only needed when all word occurrences are searched)
np = 0;
while(j < s.size())
{
if(w[k] == s[j])
{
j = j + 1;
k = k + 1;
if (k == w.size())
{
//(occurrence found, if only first occurrence is needed, m becomes j - k may be returned here)
if (np < 1000)
p[np] = j - k;
np = np + 1;
k = T[k]; //(T[length(W)] can't be -1)
}
}
else
{
k = T[k];
if (k < 0)
{
j = j + 1;
k = k + 1;
}
}
}
out << np << "\n";
for (int i = 0; i < np && i < 1000; i++)
{
out << p[i] << " ";
}
return 0;
}