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