Skip to content

Commit 55f3532

Browse files
committed
sms
1 parent 7fe2c58 commit 55f3532

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

infoarena/infoarena sms.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)