Canadian Computing Competition: 2020 Stage 1, Senior #3 You're given a string N, called the needle, and a string H, called the haystack, both of which contain only lowercase letters a..z.
Write a program to count the number of distinct permutations of N which appear as a substring of H at least once. Note that N can have anywhere between 1 and |N|! distinct permutations in total – for example, the string aab has 3 distinct permutations (aab, aba, and baa).
Problem: my program runs perfectly fine for testcase 1-113, yet no matter how I revise my program, the CCC grader will always show Receive Signal 6 at testcase 114.
#include <bits/stdc++.h>
using namespace std;
int alpha_a[26], alpha_b[26];
bool check() {
for (int i = 0; i < 26; ++i) {
if (alpha_a[i] != alpha_b[i]) {
return false;
}
}
return true;
}
int main() {
string a, b;
cin >> a >> b;
if (b.size() < a.size()) {
cout << "0" << endl;
return 0;
}
memset(alpha_a, 0, 26);
memset(alpha_b, 0, 26);
for (int i = 0; i < a.size(); ++i) {
++alpha_a[a[i] - 'a'];
++alpha_b[b[i] - 'a'];
}
string process = b.substr(0, a.size());
set<string> no_repetition = {};
int begin = 0, end = a.size();
for (; end != b.size(); ++end, ++begin) {
if (check()) {
no_repetition.emplace(process);
}
process.erase(0, 1);
--alpha_b[b[begin] - 'a'];
process += b[end];
++alpha_b[b[end] - 'a'];
}
if (check()) {
no_repetition.emplace(process);
}
cout << no_repetition.size() << endl;
return 0;
}