summaryrefslogtreecommitdiffstats
path: root/Main/C++/2020/S3.cpp
blob: 7a4f948fa7fa956661c4de85ee9d6f4032f26de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
//template<typename K, typename V>
//void print_map(std::unordered_map<K, V> const &m)
//{
//    for (auto const &pair: m) {
////        if (pair.second == 0)
////        {
////            continue;
////        }
//        std::cout << "{" << pair.first << ": " << pair.second << "}\n";
//    }
//}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string N, H;
    cin >> N;
    cin >> H;
    unordered_map<size_t, bool> seen;
    unordered_map<char, int> letter_count, needle_count;
    int x = 0;
    for(char c : H) {

        needle_count[H[x]] = 0;
        letter_count[H[x]] = 0;
        x++;
    }
    x = 0;
        for(char c : N) {
        needle_count[N[x]] += 1;
        letter_count[H[x]] += 1;
        x++;
    }

    int skip = 0;

    int output = 0;
    for (int i = N.size() - 1; i < H.size(); i++)
    {
        if (i != N.size()-1)
        {
            char start = H[i-N.size()];
            letter_count[start]--;
            letter_count[H[i]]+=1;
        }

        if (skip > 0)
        {
            skip--;
            continue;
        }
        if (needle_count[H[i]] == 0)
        {
            skip = N.size() - 1;
            continue;
        }

        string buffer = H.substr(i - N.size() + 1, N.size());
//        cout << buffer << endl;
//        print_map(letter_count);
//        cout<< endl;
//        print_map(needle_count);

        hash<string> hasher;
        size_t hash = hasher(buffer);

        if (letter_count == needle_count && !seen[hash])
        {
            output++;
            seen[hash] = true;
        }

    }
    cout << output;
    return 0;
}