Advertisement
Guest User

Temirulan

a guest
May 10th, 2015
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. double calc1(int ind, int suff) {
  2.     if(ind == s) return 0;
  3.     double &res = dp1[ind][suff];
  4.     if(calced[ind][suff]) return res;
  5.     res = 0;
  6.     calced[ind][suff] = 1;
  7.     for(int i = 0; i < 26; i++) {
  8.         char c = char(i + 'A');
  9.         if(cnt[i]) {
  10.             double p = cnt[i] / (1.0 * k);
  11.             if(c == b[suff]) {
  12.                 int cur = 0;
  13.                 int nsuff = suff;
  14.                 if(suff + 1 == sz(b)) {
  15.                     cur = 1;
  16.                     nsuff = get(b, b);
  17.                 } else {
  18.                     cur = 0;
  19.                     nsuff = suff+1;
  20.                 }
  21.                 res += p * (cur + calc1(ind+1, nsuff));
  22.             } else {
  23.                 int cur = 0;
  24.                 string cursuff = "";
  25.                 if(suff) cursuff += b.substr(0, suff);
  26.                 cursuff += c;
  27.                 int nsuff = get(b, cursuff);
  28.                 res += p * calc1(ind+1, nsuff);
  29.             }
  30.         }
  31.     }
  32.     return res;
  33. }
  34.  
  35. //here is string b is the target string
  36. //cnt[i] - appearance of char i
  37. //k - length of keyboard
  38. //s - is the length of typing string
  39. //get(string a, string b) - returns the maximum length of suffix of b which is prefix of a.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement