题目

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上大佬们的代码习惯了

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std;
const int maxn=+;
char s[maxn];
int n;
int num[];
int vis[maxn][];
char code[maxn][maxn];
int main(){
scanf("%s",s);
n=strlen(s);
for(int i=;i<n;i++)
num[s[i]-'a']++;
int ans=;
for(int k=;k<;k++){
if(num[k]){
int c=k;
int all=;
memset(code,,sizeof(code));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
if(s[i]-'a'==c){
all++; for(int j=i;j<n;j++){
vis[j-i][s[j]-'a']++;
code[all][j-i]=s[j];
}
for(int j=;j<i;j++){
vis[j+n-i][s[j]-'a']++;
code[all][j+n-i]=s[j];
}
}
}
int sum=;
for(int i=;i<n;i++){
int tem=;
for(int j=;j<=all;j++){
if(vis[i][code[j][i]-'a']==)
tem++;
}
sum=max(sum,tem);
}
ans+=sum;
}
}
printf("%.15f\n",(double)ans/n); return ;
}

codeforce468DIV2——E. Game with String的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

随机推荐

  1. 8.var目录下的文件和目录详解

    1./var目录下的文件和目录详解. /var (该目录存放的是不断扩充且经常修改的目录,包括各种日志文件或者pid文件,存放linux的启动日志和正在运行的程序目录(变化的目录:一般是日志文件,ca ...

  2. 3.了解linux系统以及搭建学习环境

    目录: 1.linux的前世今生. 2.企业如何选择linux系统? 3.如何在虚拟机上安装linux系统?搭建学习环境. 1.linux的前世今生. 1).起源:先是贝尔实验室的Unix系统,因为各 ...

  3. .net 系列化与反序列化(转载)

    .net序列化及反序列化 转载自:http://www.cnblogs.com/Tim_Liu/archive/2010/11/09/1872587.html 序列化是指一个对象的实例可以被保存,保存 ...

  4. Shell 参数(2) --解析命令行参数工具:getopts/getopt

    getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...

  5. make命令和makefile文件

    make命令和makefile文件的结合提供了一个在项目管理领域十分强大的工具,它不仅常被用于控制源代码的编译,而且还用于手册页的编写以及将应用程序安装到目标目录. makefile文件由一组依赖关系 ...

  6. N位N进制里有多少个N

    32位二进制里有多少个1 https://blog.csdn.net/zhangsj1007/article/details/81411063 有这样一道计算机问题"32位二进制里面有多少个 ...

  7. Java 开发手册之编程规约

    一.编程规约 (一) 命名规约 1.[强制] 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束.(代码规范,易读) 反例: name / __name / $Object / n ...

  8. JDBC 2 封装

    1 封装 新建类 DBConnUtil  ,新建database.properties 文件储存链接信息如下所示 jdbcDriver=com.mysql.jdbc.Driver jdbcUrl=jd ...

  9. jQuery使用prop设置checkbox全选、反选

    $(function(){     var checkbox = $("input[type='checkbox']");     //全选     $('#select-all' ...

  10. HTML中id与name的用法

    可以说几乎每个做过Web开发的人都问过,到底元素的ID和Name有什么区别阿?为什么有了ID还要有Name呢? 而同样我们也可以得到最经典的答案:ID就像是一个人的身份证号码,而Name就像是他的名字 ...