Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. long long calc(int ind, int used, int rem, bool change, int str, int used_l, const string & s, const string & p) {
  2. if (rem < 0)
  3. return 0;
  4. if (ind == p.size())
  5. return rem == 0 && !change;
  6.  
  7. long long & r = dp[used][rem][change][str];
  8. if (r != -1)
  9. return r;
  10. r = 0;
  11. int letters = 0;
  12. char cur_l;
  13.  
  14. for (int i = 0; i < (int)p.size(); ++i) {
  15. if (!((used >> i) & 1)) {
  16. cur_l = p[i];
  17. if ((letters >> (cur_l - 'a')) & 1) // don't reuse a char we used her before or u wilong long consider duplicates (cc gg case)
  18. continue;
  19.  
  20. letters |= (1 << (cur_l - 'a'));
  21. r += calc(ind + 1, used | (1 << i), rem - abs(s[ind] - cur_l), change, str, used_l | (1 << (cur_l - 'a')), s, p);
  22.  
  23. if (change) { // try 1 change for any
  24. if (!((used_l >> (cur_l - 1 - 'a')) & 1)) // don't reuse a char we used her before or u wilong long consider duplicates (cc gg case)
  25. r += calc(ind + 1, used | (1 << i), rem - abs(s[ind] - (cur_l - 1)), 0, str, used_l | (1 << (cur_l - 1 - 'a')), s, p);
  26.  
  27. if (!((used_l >> (cur_l + 1 - 'a')) & 1))
  28. r += calc(ind + 1, used | (1 << i), rem - abs(s[ind] - (cur_l + 1)), 0, str, used_l | (1 << (cur_l + 1 - 'a')), s, p);
  29. }
  30. }
  31. }
  32. return r;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement