Pagini recente » Cod sursa (job #3301780) | Cod sursa (job #70491) | Cod sursa (job #3317982) | Cod sursa (job #3328463) | Cod sursa (job #3334348)
//https://infoarena.ro/problema/strmatch
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
#define maxn 2000001
char pattern[maxn], text[maxn];
int pref[maxn], n, m, maxSolNr;
vector <int> sol;
// construim sirul de prefixe pentru pattern
void compute_prefix_table() {
// pref[1] = 0;
int k;
for(int i = 2; i <= n; i++) {
k = pref[i-1];
while(k && pattern[i] != pattern[k+1]) {
k = pref[k];
}
if(pattern[i] == pattern[k+1]) {
k ++;
}
pref[i] = k;
}
}
int main() {
int j; // pattern position
fin>>pattern+1>>text+1;
n = strlen(pattern+1);
m = strlen(text+1);
compute_prefix_table();
j = 0;
for(int i = 1; i <= m; i++) {
while(j && text[i] != pattern[j+1]) {
j = pref[j];
}
if(text[i] == pattern[j+1]) {
j++;
}
if(j == n) // we have a full pattern matching finishing at index i in the text
{
sol.push_back(i - n + 1); // val de inceput a acestui matching
}
}
fout<<sol.size()<<'\n';
maxSolNr = 0;
for(auto it:sol) {
fout<<it-1<<' '; // solutia afisata e indexata de la 0
if(++maxSolNr == 1000)
break;
}
return 0;
}