結果

問題 No.1695 Mirror Mirror
ユーザー eSeFeSeF
提出日時 2021-08-10 06:16:19
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,530 bytes
コンパイル時間 5,793 ms
コンパイル使用メモリ 149,092 KB
実行使用メモリ 12,512 KB
最終ジャッジ日時 2023-08-28 16:05:28
合計ジャッジ時間 8,212 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 129 ms
12,512 KB
testcase_22 AC 128 ms
12,512 KB
testcase_23 AC 80 ms
11,812 KB
testcase_24 AC 114 ms
11,760 KB
testcase_25 AC 20 ms
10,012 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 10 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 24 ms
10,092 KB
testcase_31 AC 37 ms
11,296 KB
testcase_32 AC 16 ms
5,356 KB
testcase_33 AC 80 ms
11,596 KB
testcase_34 AC 39 ms
7,440 KB
testcase_35 AC 70 ms
11,108 KB
testcase_36 AC 80 ms
12,124 KB
testcase_37 AC 36 ms
7,004 KB
testcase_38 AC 81 ms
10,936 KB
testcase_39 AC 27 ms
6,864 KB
testcase_40 AC 93 ms
11,612 KB
testcase_41 AC 112 ms
12,124 KB
testcase_42 AC 32 ms
12,296 KB
testcase_43 AC 28 ms
10,000 KB
testcase_44 AC 25 ms
10,640 KB
testcase_45 AC 20 ms
9,840 KB
testcase_46 AC 68 ms
10,328 KB
testcase_47 AC 57 ms
10,096 KB
testcase_48 AC 70 ms
11,680 KB
testcase_49 AC 98 ms
12,368 KB
testcase_50 AC 14 ms
4,376 KB
testcase_51 AC 16 ms
4,436 KB
testcase_52 AC 10 ms
4,380 KB
testcase_53 AC 10 ms
4,376 KB
testcase_54 AC 10 ms
4,380 KB
testcase_55 AC 23 ms
9,660 KB
testcase_56 AC 25 ms
10,508 KB
testcase_57 AC 40 ms
9,768 KB
testcase_58 AC 31 ms
11,028 KB
testcase_59 AC 28 ms
10,488 KB
testcase_60 AC 27 ms
9,760 KB
testcase_61 AC 13 ms
7,056 KB
testcase_62 AC 20 ms
11,804 KB
testcase_63 AC 35 ms
11,964 KB
testcase_64 AC 1 ms
4,380 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);
    int INF = 1<<30;
    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