forked from Onlian0/FEFU_second_semester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD1.cpp
More file actions
78 lines (66 loc) · 1.67 KB
/
D1.cpp
File metadata and controls
78 lines (66 loc) · 1.67 KB
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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
int f(int n)
{
unsigned long long result = 1;
for (int i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
bool isValidInput(const string &S, const string &P)
{
// Проверяем, что длина строки P не превышает длину строки S
if (P.size() > S.size())
{
return false;
}
// Проверяем, что все буквы в строке P различны
vector<short> a(128);
for (char c : P)
{
a[c]++;
if (a[c] > 1)
{
return false;
}
}
// Проверяем, что все буквы в строке P содержатся в строке S
for (char c : P)
{
if (S.find(c) == string::npos)
{
return false;
}
}
return true;
}
int countSubstringWords(const string &S, const string &P)
{
int result = 0;
int N = S.size();
int M = P.size();
for (int i = 0; i <= N - M; i++)
{
result += f(N - M) / f(N - M - i) * (i + 1);
}
return result;
}
int main()
{
string S, P;
fin >> S >> P;
// Проверяем валидность входных данных
if (!isValidInput(S, P))
{
fout << 0;
return 0;
}
// Подсчитываем количество слов, содержащих подстроку P, и выводим результат
int count = countSubstringWords(S, P);
fout << count << endl;
return 0;
}