Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node{
- int is_end, bad_let;
- node* next[26];
- node(int bad_len) : bad_let(bad_let), is_end(0) {
- memset(next, 0, sizeof(next));
- }
- };
- typedef node * pnode;
- int bad[30];
- void add_string(pnode v, string &s) {
- forn(i, (int)s.size()) {
- if (!v->next[s[i]])
- v->next[s[i]] = new node(bad[s[i]]);
- v = v->next[s[i]];
- }
- v->is_end = 1;
- return;
- }
- int dfs(pnode v, int k, int sum = 0) {
- if (!v)
- return 0;
- sum += v->bad_let;
- if (sum > k)
- return 0;
- int ans = 1;
- forn(i, 26) {
- ans += dfs(v->next[i], k, sum);
- return ans;
- }
- void solve() {
- string s, a;
- int k;
- cin >> s >> a >> k;
- forn(i, (int)a.size())
- bad[i] = (a[i] - '0');
- int n = (int)s.size();
- forn(i, n)
- s[i] -= 'a';
- pnode root = new node(0);
- forn(i, n) {
- string cur;
- for (int j = i; j < n; ++j)
- cur += s[j];
- add_string(root, cur);
- }
- cout << dfs(root, k) - 1;
- }
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement