Submission #1075920

# Submission time Handle Problem Language Result Execution time Memory
1075920 2024-08-26T09:48:28 Z vjudge1 Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
190 ms 512 KB
#include <bits/stdc++.h>

using namespace std;

#define task "test"
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define vii vector <ii>
#define vi vector <int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll

void solve();

int32_t main() {
    if(fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
	cin.tie(0)->sync_with_stdio(0);

    solve();
}

const int N = 3005;

int lenS, lenT, kmpn[N], kmp[N], matchn[N], match[N], res, p1, p2; bool isFlip;
string s, t;

void _solve() {
    FOR(j, 0, lenT) {
        /// j, j+1
        kmpn[j] = kmp[j+1] = 0;
        FORD(i, j-1, 1) {
            int tmp = kmpn[i+1];
            while(tmp > 0 && t[i] != t[j-tmp]) tmp = kmpn[j-tmp+1];
            kmpn[i] = (t[i] == t[j-tmp]) ? ++tmp : 0;
        }
        FOR(i, j+2, lenT) {
            int tmp = kmp[i-1];
            while(tmp > 0 && t[i] != t[j+1+tmp]) tmp = kmp[j+tmp];
            kmp[i] = (t[i] == t[j+1+tmp]) ? ++tmp : 0;
        }

        match[0] = 0;
        FOR(i, 1, lenS) {
            int tmp = match[i-1];
            while(tmp > 0 && s[i] != t[j+1+tmp]) tmp = kmp[j+tmp];
            match[i] = (s[i] == t[j+1+tmp]) ? ++tmp : 0;
        }
        matchn[lenS+1] = 0;
        FORD(i, lenS, 1) {
            int tmp = matchn[i+1];
            while(tmp > 0 && s[i] != t[j-tmp]) tmp = kmpn[j-tmp+1];
            matchn[i] = (s[i] == t[j-tmp]) ? ++tmp : 0;
        }

        FOR(i, 0, lenS) {
            if(match[i] + matchn[i+1] > res) {
                res = match[i]+matchn[i+1];

                p1 = i-match[i]+1;
                if(isFlip) p1 = (lenS-p1+1)-res+1;
                p2 = j-matchn[i+1]+1;

//                cout << i << " " << j << " " << res << '\n';
            }
        }
    }
}

void solve() {
    cin >> s >> t;
    lenS = s.length(); lenT = t.length();
    s = " " + s; t = " " + t;

    _solve();
    reverse(s.begin()+1, s.end()); isFlip = 1;
//    cout << s << '\n' << t << '\n';
    _solve();

    cout << res << '\n';
    cout << p1-1 << " " << p2-1 << '\n';
}

Compilation message

necklace.cpp: In function 'int32_t main()':
necklace.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
necklace.cpp:26:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 165 ms 496 KB Output is correct
2 Correct 121 ms 508 KB Output is correct
3 Correct 190 ms 348 KB Output is correct
4 Correct 117 ms 508 KB Output is correct
5 Correct 77 ms 504 KB Output is correct
6 Correct 85 ms 504 KB Output is correct
7 Correct 111 ms 348 KB Output is correct
8 Correct 132 ms 504 KB Output is correct
9 Correct 149 ms 512 KB Output is correct