結果

問題 No.2254 Reverse Only
ユーザー kyawakyawa
提出日時 2023-03-24 23:32:00
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 1,869 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 211,508 KB
実行使用メモリ 25,112 KB
最終ジャッジ日時 2024-09-18 17:39:45
合計ジャッジ時間 8,225 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

template <class T>
vector<int64_t> zalgo(vector<T> &s) {
    int64_t N = s.size();
    vector<int64_t> res(N);
    res[0] = 0;
    for (int64_t i = 1, j = 0; i < N; i++) {
        int64_t& k = res[i];
        k = (i < j + res[j]) ? min(j + res[j] - i, res[i - j]) : 0;
        while (i + k < N and s[k] == s[i + k]) k++;
        if (j + res[j] < i + res[i]) j = i;
    }
    res[0] = N;
    return res;
}

int main(){
    int64_t N, K; cin >> N >> K;
    vector<int64_t> A(N), B(N);
    for(auto &a : A) cin >> a;
    for(auto &b : B) cin >> b;
    
    if(K > N){
        cout << (A == B ? "Yes" : "No") << '\n';
        return 0;
    }
    if(K == N){
        vector<int64_t> C = A;
        reverse(C.begin(), C.end());
        cout << (A == B or C == B ? "Yes" : "No") << '\n';
        return 0;
    }
    if(K == N-1){
        vector<int64_t> BAA = B;
        for(auto a : A) BAA.push_back(-1);
        for(auto a : A) BAA.push_back(a);
        for(auto a : A) BAA.push_back(a);
        auto Z_BAA = zalgo<int64_t>(BAA);
        for(int64_t i = 2*N; i < 3*N; i++){
            if(Z_BAA[i] >= N){
                cout << "Yes" << '\n';
                return 0;
            }
        }
        reverse(B.begin(), B.end());
        BAA = B;
        for(auto a : A) BAA.push_back(-1);
        for(auto a : A) BAA.push_back(a);
        for(auto a : A) BAA.push_back(a);
        Z_BAA = zalgo<int64_t>(BAA);
        for(int64_t i = 2*N; i < 3*N; i++){
            if(Z_BAA[i] >= N){
                cout << "Yes" << '\n';
                return 0;
            }
        }
        cout << "No" << '\n';
        return 0;
    }
    if(K < N-1){
        vector<int64_t> C = A, D = B;
        sort(C.begin(), C.end());
        sort(D.begin(), D.end());
        cout << (C == D ? "Yes" : "No") << '\n';
        return 0;
    }
}
0