文章

1

粉丝

0

获赞

2

访问

111

头像
删除字符串 题解:
P1026 贵州大学机试题
发布于2026年3月4日 22:27
阅读数 111

为了写kmp而写kmp哈哈哈

//
// Created by 67234 on 2026/3/4.
//

//只为手搓kmp
//删除应该从后往前删不然下标会错位
#include<bits/stdc++.h>
using namespace std;

vector<int> getNext(string s)
{
    vector<int> next(s.size(), 0);
    for (int i = 1, j = 0; i < s.size(); i++)
    {
        while (j > 0 && s[i] != s[j]) j = next[j - 1];
        if (s[i] == s[j])j++;
        next[i] = j;
    }
    return next;
}

vector<int> getStrIndex(string m, string s)
{
    vector<int> next = getNext(s);
    vector<int> res;

    int j = 0;
    for (int i = 0; i < m.size(); i++)
    {
        while (j > 0 && m[i] != s[j]) j = next[j - 1];
        if (m[i] == s[j])
        {
            j++;
        }
        if (j == s.size())
        {
            res.push_back(i - j + 1);
            j = next[j - 1];
        }
    }
    return res;
}

int main()
{
    string s;
    getline(cin, s);
    if (s.size() < 3)
    {
...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发