当前位置 : 主页 > 编程语言 > c语言 >

CodeForces - 931E 概率dp

来源:互联网 收集:自由互联 发布时间:2023-09-07
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 Vasy

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.

题意:一个人将一个字符串的后面部分接到前面,告诉另一个人首字母,另一个人还可以再询问一次某一个位置的字符,问其可以获胜的概率;

思路:考虑 dp[ i ][ j ][ k ] 表示字母 i,j 距离k 次在原字符串中出现了多少次,那么我们只需要找到多少个满足 dp[ i ][ j ][ k ]==1 的即可,因为这是可以唯一确定的,不能唯一确定的肯定会输:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>

typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}




int dp[100][100][6000];

int main()
{
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int len = s.length();
    for (int i = 0; i < len; i++) {
        for (int k = 1; k < len; k++) {
            dp[s[i] - 'a'][s[(i + k) % len] - 'a'][k]++;
        }
    }
    double ans = 0.0;
    for (int i = 0; i < 26; i++) {
        int maxx = 0;
        for (int k = 1; k < len; k++) {
            int tmp = 0;
            for (int j = 0; j < 26; j++) {
                if (dp[i][j][k] == 1)
                    tmp++;
            }
            maxx = max(maxx, tmp);
        }
        ans += 1.0*maxx / len;
    }
    printf("%.8f\n", ans*1.000);
}
【本文由: 阜宁网页制作 http://www.1234xp.com/funing.html 复制请保留原URL】
上一篇:C. Equal Sums map映射
下一篇:没有了
网友评论