結果

問題 No.1695 Mirror Mirror
ユーザー eSeFeSeF
提出日時 2021-09-14 01:16:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 6,070 ms
コンパイル使用メモリ 265,976 KB
実行使用メモリ 22,088 KB
最終ジャッジ日時 2023-08-28 16:05:39
合計ジャッジ時間 8,673 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,500 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 59 ms
22,020 KB
testcase_22 AC 54 ms
22,088 KB
testcase_23 AC 49 ms
21,432 KB
testcase_24 AC 48 ms
21,536 KB
testcase_25 AC 31 ms
15,652 KB
testcase_26 AC 1 ms
4,380 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 33 ms
15,564 KB
testcase_31 AC 45 ms
20,444 KB
testcase_32 AC 15 ms
7,604 KB
testcase_33 AC 54 ms
21,100 KB
testcase_34 AC 30 ms
11,160 KB
testcase_35 AC 49 ms
18,656 KB
testcase_36 AC 52 ms
21,812 KB
testcase_37 AC 23 ms
9,896 KB
testcase_38 AC 47 ms
18,752 KB
testcase_39 AC 21 ms
8,836 KB
testcase_40 AC 46 ms
20,168 KB
testcase_41 AC 52 ms
21,712 KB
testcase_42 AC 46 ms
21,940 KB
testcase_43 AC 35 ms
14,932 KB
testcase_44 AC 36 ms
17,728 KB
testcase_45 AC 28 ms
14,484 KB
testcase_46 AC 38 ms
16,852 KB
testcase_47 AC 38 ms
16,104 KB
testcase_48 AC 53 ms
21,016 KB
testcase_49 AC 53 ms
21,976 KB
testcase_50 AC 13 ms
4,376 KB
testcase_51 AC 16 ms
4,460 KB
testcase_52 AC 10 ms
4,380 KB
testcase_53 AC 10 ms
4,380 KB
testcase_54 AC 9 ms
4,376 KB
testcase_55 AC 30 ms
14,328 KB
testcase_56 AC 32 ms
17,096 KB
testcase_57 AC 30 ms
14,932 KB
testcase_58 AC 46 ms
19,208 KB
testcase_59 AC 38 ms
17,132 KB
testcase_60 AC 28 ms
14,992 KB
testcase_61 AC 19 ms
10,352 KB
testcase_62 AC 40 ms
21,248 KB
testcase_63 AC 43 ms
21,896 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>dp(M,INF);
    for(int i=0;i<N;i++){
        if(i>=M||S[i]!=T[i])break;
        dp[i] = 1;
    }
    for(int i=0;i<N;i++){
        if(i>=M||S[N-1-i]!=T[i])break;
        dp[i] = 1;
    }
    
    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;
    }

    vector<vector<int>>G(M,vector<int>());
    for(int i=0;i<M-1;i++){
      G[i+1].push_back((i<<1));
      if(R[i]>0){
        G[i].push_back(((i+R[i])<<1)|1);
      }
    }
    deque<int>bfs;
    for(int i=0;i<M;i++){
      if(dp[i]==1){
        bfs.push_front(i);
      }
    }
    while(!bfs.empty()){
      int v = bfs.front();
      bfs.pop_front();

      for(int nxt:G[v]){
        int c = nxt&1;
        int nx = nxt>>1;
        if(dp[nx]>dp[v]+c){
          dp[nx] = dp[v] + c;
          if(c)bfs.push_back(nx);
          else bfs.push_front(nx);
        }
      }
    }

    if(dp[M-1]==INF)cout << -1 << endl;
    else cout << dp[M-1] << endl;
    

    return 0;
}
0