結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 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 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 60 ms
22,144 KB
testcase_22 AC 55 ms
22,088 KB
testcase_23 AC 50 ms
21,672 KB
testcase_24 AC 51 ms
21,496 KB
testcase_25 AC 31 ms
15,716 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 11 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 34 ms
15,524 KB
testcase_31 AC 44 ms
20,432 KB
testcase_32 AC 16 ms
7,372 KB
testcase_33 AC 54 ms
21,096 KB
testcase_34 AC 28 ms
11,092 KB
testcase_35 AC 49 ms
18,524 KB
testcase_36 AC 52 ms
21,752 KB
testcase_37 AC 23 ms
10,080 KB
testcase_38 AC 45 ms
18,752 KB
testcase_39 AC 21 ms
9,072 KB
testcase_40 AC 46 ms
20,112 KB
testcase_41 AC 51 ms
21,832 KB
testcase_42 AC 46 ms
21,880 KB
testcase_43 AC 35 ms
14,968 KB
testcase_44 AC 37 ms
17,532 KB
testcase_45 AC 29 ms
14,496 KB
testcase_46 AC 40 ms
16,764 KB
testcase_47 AC 38 ms
16,068 KB
testcase_48 AC 52 ms
21,076 KB
testcase_49 AC 54 ms
21,904 KB
testcase_50 AC 13 ms
4,376 KB
testcase_51 AC 17 ms
4,408 KB
testcase_52 AC 10 ms
4,380 KB
testcase_53 AC 10 ms
4,380 KB
testcase_54 AC 10 ms
4,376 KB
testcase_55 AC 29 ms
14,272 KB
testcase_56 AC 31 ms
17,244 KB
testcase_57 AC 30 ms
14,980 KB
testcase_58 AC 42 ms
19,344 KB
testcase_59 AC 38 ms
17,020 KB
testcase_60 AC 29 ms
14,976 KB
testcase_61 AC 20 ms
10,336 KB
testcase_62 AC 41 ms
21,572 KB
testcase_63 AC 44 ms
21,844 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 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