Mai intai trebuie sa te autentifici.
Cod sursa(job #2969597)
Utilizator | Data | 23 ianuarie 2023 14:29:15 | |
---|---|---|---|
Problema | Potrivirea sirurilor | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.95 kb |
#include<bits/stdc++.h>
#define MAX 2000005
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int m, n, k, c, urm[MAX];
char P[MAX], T[MAX];
///P- pattern T -sir
int a[MAX];
void Prefix()
{
int j = 0;
for (int i = 1; i < n; ++i)
{
while (j > 0 && P[i] != P[j])
j = urm[j - 1];
if (P[i] == P[j]) j++;
urm[i] = j;
}
}
void KMP()
{
int q = 0;
for (int i = 0; i < m; ++i)
{
while (q > 0 && P[q] != T[i])
q = urm[q - 1];
if (P[q] == T[i])
++q; ///s-au potrivit
if (q == n)
{
a[c++] = i - n + 1;
}
}
}
void Citire()
{
fin.getline(P, MAX);
fin.getline(T, MAX);
n = strlen(P);
m = strlen(T);
}
int main()
{
Citire();
Prefix();
KMP();
fout << c << '\n';
if (c > 1000)c = 1000;
for (int i = 0; i < c; ++i)
{
fout << a[i] << ' ';
}
return 0;
}