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.
分析
题目中说,告诉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的更多相关文章
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
- js报错: Uncaught RangeError: Invalid string length
在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- JavaScript中String对象的方法介绍
1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
随机推荐
- Win7系统64位环境下使用Apache——Apache2.4版本安装及卸载
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/70255992 本文出自[我是干勾鱼的博客] 之前在Win7系统64位环境下使用A ...
- C++中strftime()的详细说明
我们可以使用strftime()函数将时间格式化为我们想要的格式.它的原型如下: size_t strftime( char *strDest, size_t maxsize, const char ...
- 21天学通C++_Day4
0.迭代器 昨天晚上3G移动通信实验的时候,需要写一些简单的C程序,用到for循环的时候,发现在不同的for循环中,若定义标识符相同的变量名时,会有报错,环境是VC6: 可是一想到在for语句声明的迭 ...
- Windows下运行Hadoop
Windows下运行Hadoop,通常有两种方式:一种是用VM方式安装一个Linux操作系统,这样基本可以实现全Linux环境的Hadoop运行:另一种是通过Cygwin模拟Linux环境.后者的好处 ...
- 在JVM中,新生代和旧生代有何区别?GC的回收方式有几种?server和client有和区别?
在JVM中,新生代和旧生代有何区别?GC的回收方式有几种?server和client有和区别? 2014-04-12 12:09 7226人阅读 评论(0) 收藏 举报 分类: J2SE(5) 一 ...
- JS 隔行变色
<script type="text/javascript"> window.onload=function(){ var oUl= ...
- matlab与modelsim中的文件操作函数
matlab中 fscanf和fpintf是一对,用fprintf写的必须用fscanf来读. fread和fwrite是一对,用fwrite写的必须用fread来读. 同样的数据,使用fprintf ...
- python操作RabbitMQ(不错)
一.rabbitmq RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列 ...
- thinkphp的select和find的区别(转)
做普通PHP项目转thinkphp时,字段自动完整匹配,ajax时前台数据一直取不到,后发现是select和find返回数据集有差异,参考下面方法修改. $this->ajaxReturn($m ...
- bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1415 乍一看和“游走”一样.于是高斯消元.n^2状态,复杂度n^6…… 看看TJ,发现因为聪 ...