跳到正文

个人记录

Participants 2

_Auror_约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

我们只需用KMP与31进行比较即可 代码如下:

C
#include<bits/stdc++.h>
using namespace std;
int nxt[100001];
int sum;
void getnext(string t){
    int i=0,j=-1;
    int tl=t.size();
    nxt[0]=-1;
    while(i<tl){
        if(j==-1 || t[i]==t[j]) nxt[++i]=++j;
        else j=nxt[j];
    }
    return;
}
int kmp(string s,string t,int pos){
    int sl=s.size(),tl=t.size();
    int i=pos,j=0;
    getnext(t);
    while(i<sl && j<tl){
        if(j==-1 || s[i]==t[j]) i++,j++;
        else j=nxt[j];
    }
    if(j>=tl) return 51;
    else return -1;
}
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    string s="51";
    cout<<kmp(s,"51",0);
    return 0;
}

简单代码:

C
#include <bits/stdc++.h>
using namespace std;
int main(){
	cout<<51<<endl;
    return 0;
}
原文