File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Infoarena sms
3+ - dp[i] = The EV of the time it takes to type character i after having typed i - 1 characters
4+ = pC[s[i] - 'a'] * 1 + pI[s[i] - 'a'] * (dp[i] + 2) + pB[s[i] - 'a'] * (dp[i - 1] + dp[i] + 1)
5+ = (1 + pI[s[i] - 'a'] + pB[s[i] - 'a'] * dp[i - 1]) / pC[s[i] - 'a']
6+ - By linearity of expectation, the answer = sum(dp[i])
7+ - Complexity: O(N)
8+ */
9+
10+ #include < bits/stdc++.h>
11+ using namespace std ;
12+
13+ double pC[26 ], pI[26 ], pB[26 ];
14+
15+ int main () {
16+ ifstream cin (" sms.in" );
17+ ofstream cout (" sms.out" );
18+ int n, k;
19+ string s;
20+ cin >> n >> k >> s;
21+ for (int i = 0 ; i < k; i++) cin >> pC[i] >> pI[i] >> pB[i];
22+ double ans = 0 , prv = 0 ;
23+ for (int i = 0 ; i < n; i++) {
24+ double nxt = (1 + pI[s[i] - ' a' ] + pB[s[i] - ' a' ] * prv) / pC[s[i] - ' a' ];
25+ ans += nxt;
26+ prv = nxt;
27+ }
28+ cout << fixed << setprecision (6 ) << ans;
29+ return 0 ;
30+ }
You can’t perform that action at this time.
0 commit comments