- 题目链接:http://codeforces.com/contest/930/problem/B
- 题意:告诉你一个字符串s。我把它截成两段,再把左半段移到右边,构成一个新的字符串t,并且只把t的第一个字符 t[0] 告诉你。你还能得知一个字符 t[x],这个x是由你自己定的。当你能通过 t[0] , t[x] 和 s 完整的唯一的猜测出字符串 t 时,你获胜。否则你失败。 问你获胜的最大概率。
- 思路:先确定每个开头字符的所有相同字符的位置 xi,再从0~n-1遍历长度 d,使得对于每个确定的d, 找到 满足“( s[xi], s[xi+d])==1 ” 的xi的最大数量。也就是说对于每个开头字符我都要找到一个最大获胜概率的d,这样当我知道 t[0] 时我就直接猜测t[0+d],如此一来我的获胜概率最大。
- 亮点操作:s+=s; 这样我就遍历d时 ,就不需要循环遍历,直接往右遍历就好啦
#include <bits/stdc++.h>
#define pi acos(-1)
#define fastcin ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const LL ll_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1000 + 10;
const int mod = 1e9 + 7;
vector<int> vec[30];
int mach[30];
int main()
{
string s; cin >> s;
int len = s.size();
s+=s;
for(int i=0; i<len; i++){
vec[s[i]-'a'].push_back(i);
}
int ans =0;
for(int i=0; i<26; i++){
int maxx=0;
for(int j=0; j<len; j++){
for(int k=0; k<26; k++) mach[k]=0;
for(int k=0; k<vec[i].size(); k++){
mach[s[vec[i][k] + j] -'a']++;
}
int cnt=0;
for(int k=0; k<26; k++) if(mach[k]==1) cnt++;
maxx = max(maxx, cnt);
}
ans+=maxx;
}
cout.precision(16);
cout << fixed << ans/(long double)len << endl;
}