Cod sursa(job #2080888)

Utilizator YoChinezuWeng Mihai Alexandru YoChinezu Data 3 decembrie 2017 16:47:02
Problema Potrivirea sirurilor Scor 38
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#include <string>
#include <vector>

using namespace std;

//const int MOD = 666013; vom lucra % 2^64 pentru simplificare semnificativa a codului

ifstream in("strmatch.in");
ofstream out("strmatch.out");

int const lenmax = (int) 2E6;
int const base = 103;

typedef unsigned long long ull;  //ull-ul conform standardului este un % 2^64 foarte reliable prin overflow/underflow

string s, txt;
ull h[lenmax]; //h[i] = hash-ul subsirului de pe pozitia 0 pana pe pozitia i
ull htarget;
vector <int> v;

int myCharToInt(char x) {
    if('0' <= x && x <= '9') {
        return x - '0';
    } else if('a' <= x && x <= 'z') {
        return x - 'a' + 10;
    } else {
        return x - 'A' + 36;
    }
}

int main() {
    in >> s >> txt;

    ull shift = 1;
    for(int i = 0; i < s.size(); i++) {
        htarget = htarget * base + (myCharToInt(s[i]));
        shift = shift * base;
    }
    h[0] = txt[0] - 'A';
    for(int i = 1; i < txt.size(); i++) {
        h[i] = h[i - 1] * base + (myCharToInt(txt[i]));
    }

    int rasp = 0;
    for(int i = s.size() - 1; i < txt.size(); i++) {
        ull rolling = h[i] - (s.size() <= i ? h[i - s.size()] * shift : 0);
        if(htarget == rolling) {
            ++rasp;
            if(rasp < 1000)
                v.push_back(i - s.size() + 1);
        }
    }

    out << rasp << '\n';

    for(int i = 0; i < v.size(); ++ i) {
        out << v[i] << " ";
    }

    return 0;
}
/*
 a b c d f g h i

*/