Cod sursa(job #3293791)
| Utilizator | Data | 12 aprilie 2025 16:35:16 | |
|---|---|---|---|
| Problema | Potrivirea sirurilor | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.05 kb |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int lps[2000001];
string s,p;
vector<int>v;
int main()
{
int len=0,ap=0;
fin>>p>>s;
for(int i=1;i<p.size();i++)
{
if(p[i]==p[len])
{
len++;
lps[i]=len;
}
else
{
if(len)
{
len=lps[len-1];
i--;
}
}
}
len=0;
for(int i=0;i<s.size();i++)
{
if(s[i]==p[len])
{
len++;
if(len==p.size())
{
ap++;
if(ap<=1000)
v.push_back(i-p.size()+1);
len=lps[len-1];
}
}
else
{
if(len)
{
len=lps[len-1];
i--;
}
}
}
fout<<ap<<'\n';
for(int i=0;i<v.size();i++)
fout<<v[i]<<' ';
return 0;
}
