Pagini recente » Cod sursa (job #1745987) | Cod sursa (job #1953244) | Cod sursa (job #2249824) | Cod sursa (job #1424742) | Cod sursa (job #1022397)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int *match;
string word, text;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
void solve(){
match = new int[text.size()];
int q = -1, length = word.size();
for (int i = 0; i < text.size(); i++){
while (q != -1 && text[i] != word[q+1])
q = match[q];
if (text[i] == word[q+1]){
match[i] = ++q;
} else {
match[i] = q;
}
}
}
void print(){
int length = word.size() - 1;
int count = 0;
for (int i = 0; i < text.size(); i++){
if (match[i] == length)
count++;
}
g << count << '\n';
for (int i = 0; i < text.size(); i++){
if (match[i] == length)
g << i - match[i] << " ";
}
}
void read(){
f >> word;
f >> text;
}
int main() {
read();
solve();
print();
delete [] match;
return 0;
}