結果

問題 No.1695 Mirror Mirror
ユーザー eSeF
提出日時 2021-08-10 05:37:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,255 bytes
コンパイル時間 3,830 ms
コンパイル使用メモリ 254,408 KB
最終ジャッジ日時 2025-01-23 17:19:59
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32 WA * 13 TLE * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
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>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;
    }

    //manacherしない
    int flag = 0;
    stack<int>st;
    for(int i=0;i<M;i++){
        if(dp[i]!=1)break;
        st.push(i);
    }
    while(st.size()){
        int i = st.top();
        st.pop();

        for(int j=0;i-j>=0&&i+1+j<M;j++){
            if(T[i-j]!=T[i+1+j])break;
            dp[i+j+1]=min(dp[i+j+1],dp[i]+1);

            if(dp[M-1]!=INF){
                flag = 1;
                break;
            }
            st.push(i+j+1);
        }
        if(flag)break;
    }
    if(dp[M-1]==INF){
        cout << -1 << endl;
    }
    else{
        cout << dp[M-1] << endl;
    }
    

    return 0;
}
0