Slot Machines

題目描述

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 w = w1w2w3w4w5w6 where 0 ≤ w1, w2, w3, w4, w5, w6 ≤ 9 to represent one possible outcome of the slot machine.

Figure I.1. The layout of a slot machine.


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 T[i] be the i-th outcome of a slot machine. At first, there is a truly random sequence of length k,T[1],T[2],…,T[k]. Then there exists one positive number such that T[i+p] = T[i]for all possible values of i(>k). Once an attacker can find out the exact values of k and p, 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 k and p, then you can predict the tenth number. However, there are many candidates for k and p: one extreme case is k=5 and p=1, and another is k=0 and p=6. The most probable candidate is the one where both k and p are small. So, our choice is the one with the smallest k+p. If there are two or more such pairs, we pick the one where p is the smallest. With our example, after some tedious computation, we get k=1 and p=2.

Assume that you have n consecutive outcomes of a slot machine, T[1], T[2], …, T[n]. Write a program to compute the values of k and p satisfying the above-mentioned condition.
 

輸入

Your program is to read from standard input. The first line contains a positive integer n (1 ≤ n ≤ 1,000,000), representing the length of numbers we have observed up to now in the outcome sequence. The following line contains n numbers. Each of these numbers is between zero and 999,999.

 

輸出

Your program is to write to standard output. Print two integers k and p in one line.

 

樣例輸入

6
612534 3157 423 3157 423 3157

 

樣例輸出

1 2

題目大意是說給你n個數字,讓你求從第k+1個位置開始可以以p爲循環節,並讓p+k最小(相同時讓p最小)

把數字看成字符,再翻過來就可以看成拓展kmp的題目,預處理next數組,暴力枚舉起點k,O(1)處理出以當前節點爲起點的最小的循環節,再更新答案即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
int z[maxn];
int nextt[maxn];
int tmp[maxn];
int l[maxn];
int n;
void getnext()
{
    int k = -1;
    int j = 0;
    nextt[0] = -1;
    while (j < n)
    {
        if (k == -1 || tmp[j] == tmp[k])
        {
            j++;
            k++;
            nextt[j] = k;
        }
        else
            k = nextt[k];
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", &z[i]);
    int p,k;
    k = n - 1;
    p = 1;
    for (int i = 0; i < n; ++i)
        tmp[i] = z[n - i - 1];
    getnext();
    for (int i = 1; i <= n; ++i)
        l[n - i] = i - nextt[i];
 /*   for (int i=0;i<=n;++i){
        printf("%d %d\n",i,l[i]);
    }*/
    for (int i=0;i<n;++i){
        if ((i+l[i]<k+p) || ((i+l[i]==k+p) && (l[i]<p))){
            p=l[i];
            k=i;
        }
    }
    printf("%d %d\n",k,p);
    return 0;
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章