codeforces E. Game with String 概率
生活随笔
收集整理的這篇文章主要介紹了
codeforces E. Game with String 概率
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意
這道題目的敘述不好理解:
給你一個字符串ss,小a和小b都知道。現在小b要把字符串的左邊一段移動到最右邊,生成一個新的字符串s′s′,小a只知道s′s′的首字母是什么,現在小a有權利任意選擇新字符串的一個位置并掀開,然后根據這2個字符來猜測新的字符串是什么。
注意!當僅通過這2個字符不能唯一確定一個字符串的話,那么就算失敗。
求小a的勝率。
樣例解釋
bbaabaabbb
這個應該輸出0.1
因為當首字母是b時候,不管先開任何位置,都不能唯一確定新字符串。
當首字母為a的時候,當只有掀開第4個位置時候,如果被掀開的字母是a,那么可以確定新字符串是把原字符串左邊2個b移動到右邊形成的。
所以概率 =410?14=0.1410?14=0.1
題解:
定義 字符c,位置p的辨識度:所有首字母為c掀開位置為p的字符串,p位置出現次數為1的字母的個數。
- 分類將每個字母出現的位置存在各自對應的vector中。
- 然后我們便可枚舉首字母是啥,記為c,記錄mx[c][0]為···c為起始字符的可能字串,掀開某一個位置,所能得到的最大辨識度···
- 枚舉要掀開的位置p,記錄一個mx[c][p]變量,記錄掀開當前位置所產生的辨識度。
- ans+=mx[c][0]nans+=mx[c][0]n
代碼
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <unordered_map> using namespace std; const int maxn = 5555; char str[maxn]; vector<int> mp[27]; unordered_map<int,int> vis; int main(){scanf("%s",str);int n = strlen(str);for(int i = 0;i < n;++i){int c = str[i] - 'a';mp[c].push_back(i);}double ans = 0;for(int t = 0;t < 26;++t){if(mp[t].size() == 0) continue;int mx = 0;for(int i = 1;i < n;++i){int mx_tmp = 0;vis.clear();for(int j = 0;j < mp[t].size();++j){int now = str[(mp[t][j]+i)%n]-'a';vis[now] ++;}unordered_map<int,int>::iterator it;for(it = vis.begin();it != vis.end();++it){pair<int,int> p = *it;if(p.second == 1) {mx_tmp++;}}mx = max(mx,mx_tmp);}ans += double(mx)/double(n);}printf("%.10lf\n",ans);return 0; }總結
以上是生活随笔為你收集整理的codeforces E. Game with String 概率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lol低配置电脑怎么设置(lol低配置电
- 下一篇: codeforces F.F. Teod