codeforces 126B Password KMP

本文介绍了一个特殊的字符串匹配问题,即在一个给定的串中寻找最长的子串,该子串同时作为母串的前缀和后缀,并在前缀和后缀之外至少出现一次。通过构造失配函数并进行特定的遍历来高效解决此问题。

          给一个串,求一个最长子串同时是母串的前缀,后缀,并且在前缀后缀之外有出现了一次。

          首先构造好失配函数,从len开始沿失配路径走一遍并且标记沿途节点,为什么要标记,因为如果节点i在这条失配路径上,那么f[i]---i这个子串一定同时为母串的前缀和后缀,之后我们只要从len-1(排除后缀)到0枚举一边,在已标记的节点中找一个最大值就行。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=1010000;
bool ok[maxn];
int f[maxn];
char s[maxn],ss[maxn];
void getFail(char* P,int* f)
{
    int m=strlen(P);
    f[0]=0;
    f[1]=0;
    for (int i=1; i<m; i++)
    {
        int j=f[i];
        while(j && P[i]!=P[j]) j=f[j];
        f[i+1]=P[i]==P[j]?j+1:0;
    }
}
bool find(char* T,char* P,int* f)
{
    int n=strlen(T);
    int m=strlen(P);
//    getFail(P,f);
    int j=0;
    for (int i=0; i<n; i++)
    {
        while(j && P[j]!=T[i]) j=f[j];
        if (P[j]==T[i]) j++;
        if (j==m) return true;
    }
    return false;
}

int main()
{
//    freopen("in.txt","r",stdin);
    while(~scanf("%s",&s))
    {

        getFail(s,f);
        int p=strlen(s);
        int l=p;
        int cnt=0;
//        for (int i=0; i<=l; i++)
//        cout<<f[i]<<" ";
//        cout<<endl;
        memset(ok,false,sizeof ok);
        while(p)
        {
            ok[f[p]]=true;
            p=f[p];
        }
        for (int i=l-1; i>0; i--)
        {
            if (ok[f[i]]) cnt=max(cnt,f[i]);
        }
        if (cnt)
        {
            for (int i=0; i<cnt; i++)
            printf("%c",s[i]);
            puts("");
        }
        else
        puts("Just a legend");
    }

    return 0;

}


### 关于 Codeforces 平台上的 KMP 算法练习题目 对于希望在 Codeforces 上找到有关 KMP (Knuth-Morris-Pratt) 字符串匹配算法的练习题目的选手来说,可以关注几个特定标签下的问题。通常这些题目会被标记为 "strings" 或者更具体地标记为 "string suffix structures"[^1]。 为了帮助更好地理解如何查找适合的练习题目,在此提供一段 Python 代码用于模拟访问 API 获取带有指定标签的问题列表: ```python import requests def fetch_problems(tag, platform="codeforces"): url = f"https://{platform}.com/api/problemset.problems?tags={tag}" response = requests.get(url) if response.status_code != 200: raise Exception(f"Failed to retrieve data from {platform}") json_data = response.json() problem_list = [] for index, item in enumerate(json_data['result']['problems']): name = item["name"] rating = item.get('rating', 'N/A') contest_id = item["contestId"] index = item["index"] problem_info = { "Name": name, "Rating": rating, "Link": f"{platform}.com/contest/{contest_id}/problem/{index}" } problem_list.append(problem_info) return problem_list[:5] # Example usage with the tag related to string processing or specifically KMP pattern matching. kmp_related_tag = "strings" fetched_problems = fetch_problems(kmp_related_tag) for prob in fetched_problems: print(prob) ``` 这段脚本会调用 Codeforces 的公开 API 来获取前五个与字符串处理相关的编程挑战链接,并打印出来供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值