結果

問題 No.1695 Mirror Mirror
ユーザー eSeF
提出日時 2021-08-10 05:26:17
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,127 bytes
コンパイル時間 3,783 ms
コンパイル使用メモリ 180,608 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-07-19 09:45:10
合計ジャッジ時間 8,013 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17 TLE * 1 -- * 43
権限があれば一括ダウンロードができます

ソースコード

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;
    for(int i=0;i<M;i++){
        if(dp[i]==INF)continue;
        for(int j=0;i+1+j<M && i-j>=0 ;j++){
            if(T[i-j] != T[i+1+j])break;
            dp[i+1+j] = min(dp[i+1+j],dp[i]+1);
            if(dp[M-1]!=INF){
                flag = 1;
                break;
            }
        }
        if(flag)break;
    }
    if(dp[M-1]==INF){
        cout << -1 << endl;
    }
    else{
        cout << dp[M-1] << endl;
    }
    

    return 0;
}
0