codeforces E. Game with String 概率

题意

这道题目的叙述不好理解:
给你一个字符串 s s ,小a和小b都知道。现在小b要把字符串的左边一段移动到最右边,生成一个新的字符串s,小a只知道 s s ′ 的首字母是什么,现在小a有权利任意选择新字符串的一个位置并掀开,然后根据这2个字符来猜测新的字符串是什么。
注意!当仅通过这2个字符不能唯一确定一个字符串的话,那么就算失败。
求小a的胜率。


样例解释

bbaabaabbb
这个应该输出0.1
因为当首字母是b时候,不管先开任何位置,都不能唯一确定新字符串。
当首字母为a的时候,当只有掀开第4个位置时候,如果被掀开的字母是a,那么可以确定新字符串是把原字符串左边2个b移动到右边形成的。
所以概率 = 41014=0.1 4 10 ∗ 1 4 = 0.1


题解:

定义 字符c,位置p的辨识度:所有首字母为c掀开位置为p的字符串,p位置出现次数为1的字母的个数。

  • 分类将每个字母出现的位置存在各自对应的vector中。
  • 然后我们便可枚举首字母是啥,记为c,记录mx[c][0]为···c为起始字符的可能字串,掀开某一个位置,所能得到的最大辨识度···
  • 枚举要掀开的位置p,记录一个mx[c][p]变量,记录掀开当前位置所产生的辨识度。
  • ans+=mx[c][0]n a n s + = m x [ 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值