A - Accept or Reject Gym - 102448A(manacher)

This semester, various UFPE competitors had classes of a discipline called Theoretical Informatics. One of the major studied concepts was Turing Machines ™.

A TM is basically a system that has a tape with infinite memory cells to the right and a head pointer that is always pointing to one cell at a given moment.

A known fact in computer science is that every algorithm has an equivalent TM.

There are many TM variations and some of them has capability differences. One of these variations are the Deciders, which are machines that always stops (never entering infinite loop state), “accepting” or “rejecting” any given input.

One of the recommended exercises of the discipline was the following: “Describe a Decider that accepts a given input string of size N if it has an M sized palindrome as substring and reject if not”.

Your task here is to solve this exercise, but as we do not have compilers that can interpret TM descriptions, you instead has to code an equivalent algorithm to solve it.

Input
The input will contain only one test set. You will receive two integers N and M (1≤M≤N≤5⋅105), as described above, followed by a line with a string S of size N. It is guaranteed that S contains only lowercase english letters.

Output
The output must contain only one line with “Accept” (without quotes) if your Decider accepts the input or “Reject” (without quotes) if it doesn’t.

You may print every letter in any case you want (so, for example, the strings Accept, accept, ACcept and accEpT will all be recognized as a positive answer).

Example
Input
10 4
ajabbaaksj
Output
Accept

題意:
能否找到長度爲mm的迴文串

思路:
直接馬拉車。
不過你已經知道了迴文串長度,那你算個雙向的哈希也可以。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e6 + 7;
char s1[maxn],s2[maxn];
int f[maxn];

int manacher(int len)
{
    int ans = 0,id = 1,mx = 0;
    for(int i = 1;i < len;i++)
    {
        if(id + mx > i)f[i] = min(f[id * 2 - i],id + mx - i);
        while(i - f[i] - 1 >= 1 && i + f[i] + 1 <= len && s2[i - f[i] - 1] == s2[i + f[i] + 1])
            f[i]++;
        if(id + mx < i + f[i])
        {
            id = i;mx = f[i];
        }
        ans = max(ans,f[i]);
    }
    return ans;
}

int main()
{
    int n,m;scanf("%d%d",&n,&m);
    scanf("%s",s1 + 1);
    int len = (int)strlen(s1 + 1);
    int len2 = 0;
    for(int i = 1;i <= len;i++)
    {
        s2[++len2] = '#';
        s2[++len2] = s1[i];
    }
    s2[++len2] = '#';
    manacher(len2);
    int ans = 0;
    for(int i = 1;i <= len2;i++) {
        if(f[i] >= m && (f[i] % 2 == m % 2)) {
            printf("Accept\n");
            return 0;
        }
    }
    
    printf("Reject\n");
    return 0;
}

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