E. Game with String
Vasya and Kolya play a game with a string, using the following rules. Initially, Kolya creates a string s, consisting of small English letters, and uniformly at random chooses an integer k from a segment [0, len(s) - 1]. He tells Vasya this string s, and then shifts it k letters to the left, i. e. creates a new string t = sk + 1sk + 2… sns1s2… sk. Vasya does not know the integer k nor the string t, but he wants to guess the integer k. To do this, he asks Kolya to tell him the first letter of the new string, and then, after he sees it, open one more letter on some position, which Vasya can choose.
Vasya understands, that he can’t guarantee that he will win, but he wants to know the probability of winning, if he plays optimally. He wants you to compute this probability.
Note that Vasya wants to know the value of k uniquely, it means, that if there are at least two cyclic shifts of s that fit the information Vasya knowns, Vasya loses. Of course, at any moment of the game Vasya wants to maximize the probability of his win.
Input
The only string contains the string s of length l (3 ≤ l ≤ 5000), consisting of small English letters only.
Output
Print the only number — the answer for the problem. You answer is considered correct, if its absolute or relative error does not exceed 10 - 6.
Formally, let your answer be a, and the jury’s answer be b. Your answer is considered correct if
Examples
input
technocup
output
1.000000000000000
input
tictictactac
output
0.333333333333333
input
bbaabaabbb
output
0.100000000000000
Note
In the first example Vasya can always open the second letter after opening the first letter, and the cyclic shift is always determined uniquely.
In the second example if the first opened letter of t is “t” or “c”, then Vasya can’t guess the shift by opening only one other letter. On the other hand, if the first letter is “i” or “a”, then he can open the fourth letter and determine the shift uniquely.
Solution
给出一个字符串,然后从第k个字符切开,将其第k~(n-1)放到前面,把1~(k-1)放到后面,告知你第1个字符,并且你有一次知道随便一个位置的字符的机会,问你能猜到k是第几位的概率。从前往后遍历,假设第i位开头的时候,把后面的每一位分别为26个字母的情况记录下来,只有当情况唯一的时候k是能猜到的。
Code
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
const int maxn = 5010;
string str;
int cnt[30][30][maxn]; //cnt[a][b][c] : 首字母为a时,第c位是字母b的情况有多少种
int main() {
cin >> str;
for(int i=0; i<str.size(); i++) {
for(int j=1; j<str.size(); j++) {
cnt[str[i] - 'a'][str[(i+j)%str.size()] - 'a'][j]++;
}
}
double ans = 0.0;
for(int i=0;i<26;i++) {
double maxx = 0.0;
for(int j=1;j<str.length();j++) {
double cot = 0.0;
for(int k=0;k<26;k++) {
if(cnt[i][k][j] == 1) {
cot++;
}
}
maxx = max(maxx,cot);
}
ans += 1.0 * maxx / str.length();
}
printf("%.15lf\n",ans);
}