Pagini recente » Cod sursa (job #832172) | Cod sursa (job #3120473) | Cod sursa (job #2422220) | Cod sursa (job #3288283) | Cod sursa (job #2881429)
#include <fstream>
#include <string>
#include <vector>
#include <cctype>
#include <unordered_map>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int main()
{
string a, b;
vector <int> rez;
unordered_map <char, int> chei;
fin >> a;
fin >> b;
int nrlitalfabet = 62, prim = 1999999973, lung = a.length(), i, j, k=0;
long long hasha=0, putmic = 1, hashb = 0, hashbnou;
char test;
j = 1;
for (test = 'a'; test <= 'z'; test++, j++)
chei[test] = j;
for (test = 'A'; test <= 'Z'; test++, j++)
chei[test] = j;
for (test = '0'; test <= '9'; test++, j++)
chei[test] = j;
if (a.length() > b.length())
fout << 0;
else
{
for(i = lung-1; i >=1; i--)
{
hasha = (hasha + putmic * chei[a[i]])%prim;
hashb = (hashb + putmic * chei[b[i]])%prim;
putmic = (putmic * nrlitalfabet) % prim;
}
hasha = (hasha + putmic * chei[a[0]])%prim;
hashb = (hashb + putmic * chei[b[0]])%prim;
if (hasha == hashb)
{
rez.push_back(0);
k++;
}
hashbnou = hashb;
for (i = lung; i < b.length(); i++)
{
j = i - lung + 1;
hashbnou = ((prim + hashbnou - (chei[b[j-1]] * putmic)%prim) % prim *nrlitalfabet + (chei[b[i]]))%prim;
if (hashbnou == hasha)
{
k++;
if (k <= 1000)
rez.push_back(j);
}
}
fout << k << '\n';
for (auto i : rez)
{
fout << i << ' ';
}
}
return 0;
}