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上大佬们的代码习惯了
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\n",(double)ans/n); 52 53 return 0; 54 }