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

本文介绍了一种用于确定输入字符串中是否存在特定长度回文子串的Decider算法实现。通过马拉车算法和双向哈希方法,该算法能够有效判断给定字符串是否包含指定长度的回文串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:
能否找到长度为mmm的回文串

思路:
直接马拉车。
不过你已经知道了回文串长度,那你算个双向的哈希也可以。

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值