Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. struct node{
  2.     int is_end, bad_let;
  3.     node* next[26];
  4.     node(int bad_len) : bad_let(bad_let), is_end(0) {
  5.         memset(next, 0, sizeof(next));
  6.     }
  7. };
  8. typedef node * pnode;
  9.  
  10. int bad[30];
  11. void add_string(pnode v, string &s) {
  12.     forn(i, (int)s.size()) {
  13.         if (!v->next[s[i]])
  14.             v->next[s[i]] = new node(bad[s[i]]);
  15.         v = v->next[s[i]];
  16.     }
  17.     v->is_end = 1;
  18.     return;
  19. }
  20.  
  21. int dfs(pnode v, int k, int sum = 0) {
  22.     if (!v)
  23.         return 0;
  24.     sum += v->bad_let;
  25.     if (sum > k)
  26.         return 0;
  27.     int ans = 1;
  28.     forn(i, 26) {
  29.         ans += dfs(v->next[i], k, sum);
  30.     return ans;
  31. }
  32.  
  33. void solve() {
  34.     string s, a;
  35.     int k;
  36.     cin >> s >> a >> k;
  37.     forn(i, (int)a.size())
  38.         bad[i] = (a[i] - '0');
  39.     int n = (int)s.size();
  40.     forn(i, n)
  41.         s[i] -= 'a';
  42.     pnode root = new node(0);
  43.     forn(i, n) {
  44.         string cur;
  45.         for (int j = i; j < n; ++j)
  46.             cur += s[j];
  47.         add_string(root, cur);
  48.     }
  49.     cout << dfs(root, k) - 1;
  50. }
  51.  
  52. int main() {
  53. #ifdef  _DEBUG
  54.     freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  55. #endif
  56.     ios_base::sync_with_stdio(0);
  57.     cin.tie(0);
  58.     solve();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement