結果

問題 No.1695 Mirror Mirror
ユーザー eSeFeSeF
提出日時 2021-09-14 01:09:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,509 bytes
コンパイル時間 4,324 ms
コンパイル使用メモリ 263,452 KB
実行使用メモリ 12,448 KB
最終ジャッジ日時 2023-08-28 16:05:49
合計ジャッジ時間 8,871 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 138 ms
12,448 KB
testcase_22 AC 138 ms
12,396 KB
testcase_23 AC 86 ms
11,776 KB
testcase_24 AC 123 ms
11,704 KB
testcase_25 AC 21 ms
10,112 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 10 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 26 ms
9,960 KB
testcase_31 AC 41 ms
11,272 KB
testcase_32 AC 16 ms
5,200 KB
testcase_33 AC 86 ms
11,600 KB
testcase_34 AC 41 ms
7,336 KB
testcase_35 AC 75 ms
11,056 KB
testcase_36 AC 83 ms
12,152 KB
testcase_37 AC 39 ms
6,872 KB
testcase_38 AC 84 ms
10,836 KB
testcase_39 AC 29 ms
6,656 KB
testcase_40 AC 100 ms
11,584 KB
testcase_41 AC 121 ms
12,180 KB
testcase_42 AC 34 ms
12,176 KB
testcase_43 AC 31 ms
9,916 KB
testcase_44 AC 27 ms
10,532 KB
testcase_45 AC 22 ms
9,712 KB
testcase_46 AC 74 ms
10,276 KB
testcase_47 AC 61 ms
10,144 KB
testcase_48 AC 74 ms
11,712 KB
testcase_49 AC 106 ms
12,288 KB
testcase_50 AC 13 ms
4,376 KB
testcase_51 AC 17 ms
4,472 KB
testcase_52 AC 10 ms
4,376 KB
testcase_53 AC 10 ms
4,380 KB
testcase_54 AC 9 ms
4,380 KB
testcase_55 AC 24 ms
9,632 KB
testcase_56 AC 30 ms
10,440 KB
testcase_57 AC 43 ms
9,648 KB
testcase_58 AC 31 ms
11,104 KB
testcase_59 AC 30 ms
10,468 KB
testcase_60 AC 29 ms
9,700 KB
testcase_61 AC 14 ms
6,900 KB
testcase_62 AC 22 ms
11,716 KB
testcase_63 AC 38 ms
11,876 KB
testcase_64 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
const int INF = 1<<30;
vector<int> manacher(string s){
    int i = 0, j = 0;
    vector<int>ret(s.size());
    while(i < s.size()){
        while(i-j>=0&&i+j<s.size()&&s[i-j]==s[i+j])j++;
        ret[i] = j;
        int k = 1;
        while(i-k>=0&&k+ret[i-k]<j)ret[i+k]=ret[i-k],++k;
        i+=k;j-=k;
    }
    return ret;
}
int mn(int l,int r){
    return min(l,r);
}
int inf(){
    return INF;
}
int main(){
    int N, M;
    cin >> N >> M;
    string S, T;
    cin >> S;
    cin >> T;

    if(M%2==1){cout << -1 << endl;return 0;}
    for(int i=0;i<M/2;i++){
        if(T[i]!=T[M-1-i]){
            cout << -1 << endl;
            return 0;
        }
    }
    M/=2;
    T = T.substr(0,M);
    vector<int>v(M,INF);
    for(int i=0;i<N;i++){
        if(i>=M||S[i]!=T[i])break;
        v[i] = 1;
    }
    for(int i=0;i<N;i++){
        if(i>=M||S[N-1-i]!=T[i])break;
        v[i] = 1;
    }
    
    lazy_segtree<int,mn,inf,int,mn,mn,inf>dp(v);
    string tt = "";
    for(int i=0;i<M;i++){
        tt += T[i];
        tt += '@';
    }
    vector<int>mc = manacher(tt);
    vector<int>R(M);
    for(int i=0;i<M;i++){
        R[i] = mc[2*i+1]/2;
    }

    for(int i=0;i<M;i++){
        if(R[i]==0)continue;
        int m = dp.get(i);
        dp.apply(i+1,min(M,i+1+R[i]),m+1);
    }
    
    int ans = dp.get(M-1);
    if(ans == INF)ans = -1;
    cout << ans << endl;
    

    return 0;
}
0