Pagini recente » Atasamentele paginii Bignumber | Cod sursa (job #350572) | Cod sursa (job #1071495) | Cod sursa (job #1071594) | Cod sursa (job #1188634)
#include <fstream>
#include <vector>
#include <string>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
string P;
vector<char> Prefix;
vector<int> Apare;
int main(){
int i, j, lg, s; char x;
fin >> P; fin.get(x);
lg = P.size();
Prefix.push_back(0);
for (j = 0, i = 1; i < lg; i++){
if (P[j] == P[i]){
if (P[i] == P[i - 1]) j = Prefix[i - 1];
else j = Prefix[i - 1] + 1;
}
else j = 0;
Prefix.push_back(j);
}
j = 0; s = 0;
while (!fin.eof()){
fin.get(x);
if (P[j] == x) j++;
else if (j == 0) j = 0;
else j = Prefix[j - 1];
if (j == P.size()) Apare.push_back(s), j = Prefix[j - 1];
s++;
}
fout << Apare.size() << "\n";
for (i = 0; i < Apare.size(); i++) fout << Apare[i] + 1 - P.size() << ' ';
return 0;
}