QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#759459#8521. Pattern Search IIIllusionaryDominanceRE 189ms22872kbC++202.3kb2024-11-18 08:54:322024-11-18 08:54:33

Judging History

你现在查看的是最新测评结果

  • [2024-11-18 08:54:33]
  • 评测
  • 测评结果:RE
  • 用时:189ms
  • 内存:22872kb
  • [2024-11-18 08:54:32]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 31;
const int MAX_M = 150000 + 5;

int N, M, len[MAX_N], f[MAX_N][MAX_M];
string s[MAX_N], t;

int Match(int x, const string &S) {
    int rem = M - x;
    // fprintf(stderr, "x = %d, rem = %d\n", x, rem);
    vector <int> decomposition(0);
    for (int i = MAX_N - 1; i >= 0 && rem; i --) {
        // fprintf(stderr, "i = %d, rem = %d\n", i, rem);
        if (i > 1) {
            if (rem >= len[i - 2]) {
                rem -= len[i - 2];
                decomposition.emplace_back(i - 2);
            }else {
                i --;
            }
        }else {
            rem --;
            decomposition.emplace_back(i);
            break;
        }
    }
    assert(!rem);
    reverse(decomposition.begin(), decomposition.end());
    int cur = 1, p = 0, sum = 0;
    for (int &i = p; i < (int)decomposition.size(); i ++) {
        if (cur + f[decomposition[i]][cur] > N) break;
        sum += len[decomposition[i]];
        cur += f[decomposition[i]][cur];
    }
    if (p == (int)decomposition.size()) return 0x3f3f3f3f;
    for (int i = decomposition[p] - 1; i >= 0; i --) {
        // fprintf(stderr, "cur = %d, i = %d, f = %d, %d\n", cur, i, f[i][cur], cur + f[i][cur]);
        if (cur + f[i][cur] <= N) {
            sum += len[i];
            cur += f[i][cur];
        }
    }
    // fprintf(stderr, "cur = %d\n", cur);
    assert(cur == N);
    for (int i = x + sum; i < M; i ++) {
        if (S[i] == t[N]) return i;
    }
    assert(false);
    return 0x3f3f3f3f;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    cin >> t;
    s[0] = "b";
    s[1] = "a";
    len[0] = len[1] = 1;
    for (int i = 2; i < MAX_N; i ++) {
        s[i] = s[i - 1] + s[i - 2];
        len[i] = len[i - 1] + len[i - 2];
    }
    N = t.size();
    t = " " + t;
    for (int i = 1; i <= N; i ++) {
        f[t[i] == 'a'][i] = 1;
    }
    for (int i = 2; i < MAX_N; i ++) {
        for (int j = 1; j <= N; j ++) {
            f[i][j] = f[i - 1][j] + f[i - 2][j + f[i - 1][j]];
        }
    }
    auto &S = s[MAX_N - 1];
    int ans = 0x3f3f3f3f; M = S.size();
    for (int i = 0; i < M; i ++) {
        ans = min(ans, Match(i, S) - i + 1);
    }
    cout << ans << '\n';
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 189ms
memory: 22872kb

input:

aabbaab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: -100
Runtime Error

input:

a

output:


result: