CodeForces 931E Game with String

Game with String

题意:有一个字符串,可以选择从第K位开始,将[K,len(s)-1]的字符都移到前面去,现在给你一个首字母,你可以再选择一位进行观察,然后猜测这个K的值是多少, 现在要求求出能猜对K的概率是多少。

题解:处理出每一个字母开头的第K位是什么字符, 如果这个字符在这个字母中出现的次数为1,那么表示可以通过这一个位置来区分字符串,cnt++,不为一就说明不能区分,那就将上次的计数删除。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 1e5+5;
14 char str[N];
15 int vis[26][26];
16 int Max[N][26];
17 int main(){
18     scanf("%s", str);
19     int ans = 0;
20     int len = strlen(str);
21     for(int to = 1; to < len; to++){
22         memset(vis,0,sizeof(vis));
23         int cnt = 0;
24         for(int i = 0; i < len; i++){
25             int p = str[i]-'a';
26             int j = i+to;
27             if(j >= len) j -= len;
28             if(vis[p][str[j]-'a'] == 0) Max[to][p]++, vis[p][str[j]-'a'] = 1;
29             else if(vis[p][str[j]-'a'] == 1) {Max[to][p]--, vis[p][str[j]-'a'] = 2;}
30         }
31     }
32     for(int j = 0; j < 26; j++){
33         int cnt = 0;
34         for(int i = 0; i < len; i++){
35             if(Max[i][j] > cnt) cnt = Max[i][j];
36         }
37         ans += cnt;
38     }
39     printf("%f",1.0*ans/len);
40     return 0;
41 }

 

转载于:https://www.cnblogs.com/MingSD/p/8523251.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值