Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- long long m = 10e9 + 7;
- long long add(const long long& a, const long long& b) {
- if (a + b > m) {
- return a + b - m;
- }
- return a + b;
- }
- long long mult(const long long& a, const long long& b) {
- return(a * b) % m;
- }
- long long sub(const long long& a, const long long& b) {
- if (a - b < m) {
- return a - b + m;
- }
- return a - b;
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- string str, str2;
- long long hash2;
- int p = 239;
- fin >> str >> str2;
- vector<long long>hash(str.size() + 1);
- vector<long long>power(str.size() + 1);
- power[0] = 1;
- hash[0] = 0;
- for (int i = 1; i < str.size() + 1; i++) {
- power[i] = mult(power[i - 1] , p);
- hash[i] = add(mult(hash[i - 1], p), str[i - 1]);
- }
- hash2 = str2[0];
- for (int i = 1; i < str2.size(); i++) {
- hash2 = add(mult(hash2, p), str2[i]);
- }
- for (int i = str2.size(); i < hash.size(); ++i) {
- if (sub(hash[i], mult(hash[i - str2.size()], power[str2.size()])) == hash2) {
- fout << i - str2.size() + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement