답안 #140735

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
140735 2019-08-04T19:14:00 Z Minnakhmetov Necklace (Subtask 1-3) (BOI19_necklace1) C++14
0 / 85
3 ms 376 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 6005;
string a, b;
int p1[N], p2[N];

void prefixFunction(string a, const string &b, int p[N]) {
    a = a + "#" + b;

    p[0] = 0;

    for (int i = 1; i < a.size(); i++) {
        int x = p[i - 1];
        while (x > 0 && a[x] != a[i])
            x = p[x - 1];
        if (a[x] == a[i])
            x++;
        p[i] = x;
    }

    for (int i = 0; i < b.size(); i++)
        p[i] = p[a.size() - b.size() + i];
}

tuple<int, int, int> calc(string a, string b) {
    b += '$';

    int ans = -1, x, y;

    for (int i = 0; i < b.size() - 1; i++) {
        prefixFunction(b, a, p1);
        reverse(all(b));
        reverse(all(a));
        prefixFunction(b, a, p2);
        reverse(p2, p2 + a.size());
        reverse(all(b));
        reverse(all(a));        

        for (int j = -1; j < (int)a.size(); j++) {
            int cur = 0;

            if (j >= 0)
                cur += p1[j];
            if (j + 1 < a.size())
                cur += p2[j + 1];

            if (cur > ans) {
                ans = cur;
                x = (j < 0 ? 0 : j - p1[j] + 1);
                y = i - (j + 1 < a.size() ? p2[j + 1] : 0);
            }
        }

        for (int j = 0; j < b.size() - 1; j++)
            swap(b[j], b[j + 1]);
    }

    return { ans, x, y };
}

signed main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> a >> b;

    auto ans = calc(a, b);
    reverse(all(b));
    ans = max(ans, calc(a, b));

    cout << get<0>(ans) << "\n" << get<1>(ans) << " " << get<2>(ans);

    return 0;
}

Compilation message

necklace.cpp: In function 'void prefixFunction(std::__cxx11::string, const string&, int*)':
necklace.cpp:16:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < a.size(); i++) {
                     ~~^~~~~~~~~~
necklace.cpp:25:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < b.size(); i++)
                     ~~^~~~~~~~~~
necklace.cpp: In function 'std::tuple<int, int, int> calc(std::__cxx11::string, std::__cxx11::string)':
necklace.cpp:34:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < b.size() - 1; i++) {
                     ~~^~~~~~~~~~~~~~
necklace.cpp:48:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (j + 1 < a.size())
                 ~~~~~~^~~~~~~~~~
necklace.cpp:54:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 y = i - (j + 1 < a.size() ? p2[j + 1] : 0);
                          ~~~~~~^~~~~~~~~~
necklace.cpp:58:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < b.size() - 1; j++)
                         ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -