Gym 101667I Slot Machines(KMP)
Gym 101667I Slot Machines
Description
Slot machines are popular game machines in casinos. The slot machine we are considering has six places where a figure appears. By combination of figures, one may earn or lose money. There are ten kinds of figures, so we will represent a figure with a number between 0 and 9. Then we can use a six-digit number ݓ ൌ ݓଵݓଶݓଷݓସݓହݓ where 0ݓଵ, ݓଶ, ݓଷ, ݓସ, ݓହ, ݓ 9 to represent one possible outcome of the slot machine.
It is guaranteed that 000000 will never appear.Old slot machines were made up with mechanical components, but nowadays they were replaced by PC-based systems. This change made one critical flaw: they are based on pseudo-random number generators and the outcome sequences of a slot machine are periodic. Let ܶሾ݅ሿ be the ݅-th outcome of a slot machine. At first, there is a truly random sequence of length ݇, ܶሾ1ሿ, ܶሾ2ሿ,…,ܶሾ݇ሿ. Then there exists one positive number
such that ܶሾ݅ሿ ൌ ܶሾ݅ሿ for all possible values of ݅ሺ ݇ሻ. Once an attacker can find out the exact values of ݇and ,he or she can exploit this fact to beat the casino by betting a lot of money when he or she knows the outcome with a good combination in advance.
For example, you have first six numbers of outcome sequences: 612534, 3157, 423, 3157, 423, and 3157.Note that we can remove first 0’s. Therefore, 3157 represents 003157 and 423 represents 000423. You want to know its tenth number. If you know the exact values of ݇ and ,then you can predict the tenth number.
However, there are many candidates for ݇ and :one extreme case is ݇=5 and =1, and another is ݇=0 and =6. The most probable candidate is the one where both ݇ and are small. So, our choice is the one with the smallest ݇+ .If there are two or more such pairs, we pick the one where is the smallest. With our example, after some tedious computation, we get ݇=1 and =2.
Assume that you have ݊ consecutive outcomes of a slot machine, ܶሾ1ሿ, ܶሾ2ሿ, … , ܶሾ݊ሿ. Write a program to compute the values of ݇ and satisfying the above-mentioned condition.
Input
Your program is to read from standard input. The first line contains a positive integer ݊ ሺ1 ݊ 1,000,000ሻ,
representing the length of numbers we have observed up to now in the outcome sequence. The following line
contains ݊ numbers. Each of these numbers is between zero and 999,999.
Output
Your program is to write to standard output. Print two integers ݇ and in one line.
The following shows sample input and output for two test cases.
Sample Input 1
6
612534 3157 423 3157 423 3157
Output for the Sample Input 1
1 2
Sample Input 2
9
1 2 1 3 1 2 1 3 1
Output for the Sample Input 2
0 4
题解
题意
题中给出一个序列,由于随机序列在计算机生成的时候实际上是伪随机序列,所以要求给出循环节长度+开始位置的最小值
思路
其实就是寻找最小循环节的问题,最开始思路有误,想通过set维护。实际上KMP算法中的next函数可以维护该操作
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6+10;
int a[MAXN],next1[MAXN],N;
void getNext(){
int i=1,j=0;
next1[1] = 0;
while(i<=N){
if(j==0||a[i]==a[j]){
i++,j++;
next1[i]=j;
}else{
j=next1[j];
}
}
}
int main() {
scanf("%d",&N);
for(int i=N;i>=1;i--) scanf("%d",&a[i]);
getNext();
int k=INF,p=INF;
for(int i=1;i<=N;i++){
int curp=i-next1[i+1]+1,curk=N-i;
if(curp+curk<p+k) p=curp,k=curk;
}
printf("%d %d\n",k,p);
return 0;
}

浙公网安备 33010602011771号