zoukankan      html  css  js  c++  java
  • codeforce468DIV2——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. 

    分析

    题目中说,告诉Vasya新串的第一个字符,然后Vasya再找一个字符,来判断这个新串。那么我们把相同字母开头的串全部找出来,然后在这些串中找一个位置i,使得在i位置字符不同的串最多。代码写得很烂,有空得学习一下cf上大佬们的代码习惯了

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 const int maxn=5000+100;
     8 char s[maxn];
     9 int n;
    10 int num[30];
    11 int vis[maxn][30];
    12 char code[maxn][maxn];
    13 int main(){
    14     scanf("%s",s);
    15     n=strlen(s);
    16     for(int i=0;i<n;i++)
    17         num[s[i]-'a']++;
    18     int ans=0;
    19     for(int k=0;k<26;k++){
    20         if(num[k]){
    21             int c=k;
    22             int all=0;
    23             memset(code,0,sizeof(code));
    24             memset(vis,0,sizeof(vis));
    25             for(int i=0;i<n;i++){
    26                 if(s[i]-'a'==c){
    27                     all++;
    28 
    29                     for(int j=i;j<n;j++){
    30                         vis[j-i][s[j]-'a']++;
    31                         code[all][j-i]=s[j];
    32                     }
    33                     for(int j=0;j<i;j++){
    34                         vis[j+n-i][s[j]-'a']++;
    35                         code[all][j+n-i]=s[j];
    36                     }
    37                 }
    38             }
    39             int sum=0;
    40             for(int i=0;i<n;i++){
    41                 int tem=0;
    42                 for(int j=1;j<=all;j++){
    43                     if(vis[i][code[j][i]-'a']==1)
    44                         tem++;
    45                 }
    46             sum=max(sum,tem);
    47             }
    48             ans+=sum;
    49         }
    50     }
    51     printf("%.15f
    ",(double)ans/n);
    52 
    53 return 0;
    54 }
    View Code
  • 相关阅读:
    结构型模式(一) 适配器模式
    选择器
    CSS引入
    CSS语法
    CSS介绍
    HTML练习
    HTML标签嵌套规则(重点)
    HTML标签分类(重点)
    HTML标签属性
    body标签
  • 原文地址:https://www.cnblogs.com/LQLlulu/p/8785230.html
Copyright © 2011-2022 走看看