i510. 尋找子字串 - 高中生程式解題系統

本文最後更新於:2024年1月12日 下午

Zerojudge
解題紀錄

i510. 尋找子字串 - 高中生程式解題系統

Zerojudge-i510-2.cpp

// Author : ysh
// 06/27/2022 Mon  7:54:06.54
#include<bits/stdc++.h>
using namespace std;
void check(string x,int a) {
    int n = x.size();
    vector<int>f(n + 1);
    for(int i = 1;i<n;i++) {
        int p = f[i - 1];
        while(x.at(p) != x.at(i) && p > 0) p = f[p - 1];
        if(x[i] == x[p]) f[i] = p + 1;
        if(f[i] == a) {
            cout<<"Yes\npos: "<<i - ((a << 1))<<"\n";
            return;
        }
    }
    cout<<"No\n";
    return;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int a,b;
    while(cin>>a>>b) {
        string c,d;cin>>c>>d;
        check(d + " " + c,b);
    }
    return 0;
}

i510. 尋找子字串 - 高中生程式解題系統
http://mysh212.github.io/algosolution/Zerojudge-i510-2.cpp/
作者
ysh
發布於
2022年6月27日
更新於
2024年1月12日
許可協議