結果

問題 No.2254 Reverse Only
ユーザー asaringoasaringo
提出日時 2023-03-24 22:55:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 607 ms / 2,000 ms
コード長 3,024 bytes
コンパイル時間 2,419 ms
コンパイル使用メモリ 218,896 KB
実行使用メモリ 34,216 KB
最終ジャッジ日時 2023-10-18 21:24:50
合計ジャッジ時間 16,230 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 590 ms
29,360 KB
testcase_09 AC 592 ms
34,208 KB
testcase_10 AC 161 ms
23,552 KB
testcase_11 AC 589 ms
29,360 KB
testcase_12 AC 593 ms
31,736 KB
testcase_13 AC 589 ms
29,360 KB
testcase_14 AC 600 ms
34,216 KB
testcase_15 AC 607 ms
34,204 KB
testcase_16 AC 598 ms
34,100 KB
testcase_17 AC 588 ms
34,100 KB
testcase_18 AC 556 ms
29,624 KB
testcase_19 AC 234 ms
23,552 KB
testcase_20 AC 248 ms
28,284 KB
testcase_21 AC 249 ms
28,288 KB
testcase_22 AC 153 ms
28,276 KB
testcase_23 AC 170 ms
28,276 KB
testcase_24 AC 177 ms
28,180 KB
testcase_25 AC 181 ms
28,180 KB
testcase_26 AC 183 ms
28,180 KB
testcase_27 AC 9 ms
4,636 KB
testcase_28 AC 84 ms
9,480 KB
testcase_29 AC 225 ms
18,324 KB
testcase_30 AC 181 ms
14,576 KB
testcase_31 AC 90 ms
10,976 KB
testcase_32 AC 102 ms
11,844 KB
testcase_33 AC 39 ms
7,368 KB
testcase_34 AC 309 ms
22,296 KB
testcase_35 AC 172 ms
15,892 KB
testcase_36 AC 484 ms
25,664 KB
testcase_37 AC 506 ms
26,720 KB
testcase_38 AC 65 ms
9,340 KB
testcase_39 AC 427 ms
24,344 KB
testcase_40 AC 8 ms
4,616 KB
testcase_41 AC 147 ms
14,696 KB
testcase_42 AC 68 ms
9,804 KB
testcase_43 AC 333 ms
23,752 KB
testcase_44 AC 557 ms
32,344 KB
testcase_45 AC 281 ms
19,064 KB
testcase_46 AC 7 ms
4,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std ;
#define fast_input_output ios::sync_with_stdio(false); cin.tie(nullptr);
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll ;
typedef long double ld ;
typedef tuple<ll,ll,ll> TP ;
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
#define bit_count(x) __builtin_popcountll(x)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a / gcd(a,b) * b
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
#define repi(it,S) for(auto it = S.begin() ; it != S.end() ; it++)
#define pt(a) cout << a << endl
#define DEBUG(...) ; cout << #__VA_ARGS__ << endl ; for(auto x : {__VA_ARGS__}) cout << x << "  " ; cout << endl ;
#define DEBUG_LIST(...) cout << #__VA_ARGS__ << endl ; DEBUG_REP(__VA_ARGS__) ;
#define DEBUG_REP(V) cout << "{ " ; repi(itr,V) cout << *itr << ", " ; cout << "}" << endl ;
#define debug(a) cout << #a << " " << a << endl
#define all(a) a.begin(), a.end()
#define endl "\n"


// kmp をやるための前計算
vector<int> makeTable(const vector<int> s) {
    int n = s.size();
    vector<int> ret(n+1);
    ret[0] = -1;
    int j = -1;
    for(int i = 0 ; i < n ; i++){
        while(j >= 0 && s[i] != s[j]) j = ret[j] ;
        j++ ;
        ret[i+1] = j ;
    }
    return ret;
}

// str の中に word とマッチする場所のリストを返す
// ret のそれぞれの要素 el は, 「str[el] からの文字列が word と一致する」ことを示す
vector<int> kmp(const vector<int> str, const vector<int> word) {
    vector<int> table = makeTable(word), ret;
    int m = 0, i = 0, n = str.size();
    while (m+i < n) {
        if (word[i] == str[m+i]) {
            if (++i == (int)(word.size())) {
                ret.push_back(m);
                m = m+i-table[i];
                i = table[i];
            }
        }
        else {
            m = m+i-table[i];
            if (i > 0) i = table[i];
        }
    }
    return ret;
}

int n, k;

int main(){
    fast_input_output
    cin >> n >> k;
    multiset<int> S, T;
    vector<int> A(n), B(n);
    map<int,int> mp;
    rep(i,n){
        cin >> A[i];
        S.insert(A[i]);
        if(mp.count(A[i]) == 0) mp[A[i]] = i; 
        A[i] = mp[A[i]];
    }
    rep(i,n){
        cin >> B[i];
        T.insert(B[i]);
        B[i] = mp[B[i]];
    }
    if(S != T){
        cout << "No" << endl;
        return 0;
    }
    if(A == B){
        cout << "Yes" << endl;
        return 0;
    }
    if(n > k + 1 || k == 2){
        cout << "Yes" << endl;
        return 0;
    }
    if(S.size() != A.size())
    if(k == 1){
        cout << "No" << endl;
        return 0;
    }
    rep(i,n) B.push_back(B[i]);
    if(kmp(B,A).size() > 0){
        cout << "Yes" << endl;
        return 0;
    }
    reverse(all(B));
    if(kmp(B,A).size() > 0){
        cout << "Yes" << endl;
        return 0;
    }
    else cout << "No" << endl;
}
0