forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsequencesinsubstrings.cpp
42 lines (39 loc) · 1008 Bytes
/
subsequencesinsubstrings.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string s, t;
cin >> s >> t;
vector<vector<int>> indexes(26);
for(int i = 0; i < s.size(); ++i) {
indexes[s[i]-'a'].push_back(i);
}
ll res = 0;
for(int i = 0; i < s.size(); ++i) {
int pos = i;
bool possible = true;
char prevLet = '$';
for(int j = (int)t.size()-1; j >= 0; --j) {
auto &arr = indexes[t[j]-'a'];
auto it = upper_bound(arr.begin(), arr.end(), pos);
if(it == arr.begin()) {
possible = false;
break;
}
--it;
if(prevLet == t[j]) {
if(it == arr.begin()) {
possible = false;
break;
}
--it;
}
pos = *it;
prevLet = t[j];
}
if(possible) {
res += pos+1;
}
}
cout << res << '\n';
}