蒟蒻40分 MLE求助

P3435 [POI2006] OKR-Periods of Words

Konjac0629 @ 2023-08-24 09:25:47

RT,大佬们帮忙看下怎么优化

提交记录

#include <bits/stdc++.h>

using namespace std;

vector<string> all_prefix(0);

// 定义一个求所有前缀的函数

vector<string> Prefix(string s)
{
    vector<string> result;
    for (int j = 1; j <= s.length(); j++)
        result.push_back(s.substr(0, j));
    return result;
}

int maxT(string s)
{
    int maxTlen = 0;
    for (int i = s.length() - 2; i >= 0; i--)
    {
        string Q = all_prefix[i];
        string duoyu=s.substr(Q.length(),s.length()-Q.length());
        if(duoyu==s.substr(0,duoyu.length())){
            maxTlen=Q.length();
            break;
        }
    }
    return maxTlen;
}

int main()
{
    int k;
    string a;
    cin >> k >> a;
    all_prefix = Prefix(a);
    int result = 0;
    for (int i = 0; i < all_prefix.size(); i++)
    {
        result = result + maxT(all_prefix[i]);
    }
    cout << result << endl;
    return 0;
}

by KυρωVixen @ 2023-08-24 15:49:04

你这样会炸的,正解是KMP next 数组性质实现时间单 log 空间线性。


by Konjac0629 @ 2023-08-26 11:36:09

@__Thaumic_Executor__ OK,明白了,谢谢大佬!


|