結果

問題 No.1695 Mirror Mirror
ユーザー fumofumofunifumofumofuni
提出日時 2021-10-07 12:54:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 2,738 bytes
コンパイル時間 2,332 ms
コンパイル使用メモリ 212,812 KB
実行使用メモリ 25,628 KB
最終ジャッジ日時 2023-08-28 16:10:07
合計ジャッジ時間 6,396 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 1 ms
4,380 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,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 72 ms
25,628 KB
testcase_22 AC 67 ms
25,380 KB
testcase_23 AC 62 ms
25,284 KB
testcase_24 AC 63 ms
24,892 KB
testcase_25 AC 34 ms
15,032 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 10 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 36 ms
15,972 KB
testcase_31 AC 50 ms
21,500 KB
testcase_32 AC 17 ms
8,032 KB
testcase_33 AC 67 ms
24,872 KB
testcase_34 AC 35 ms
12,652 KB
testcase_35 AC 59 ms
21,612 KB
testcase_36 AC 64 ms
25,444 KB
testcase_37 AC 27 ms
10,768 KB
testcase_38 AC 53 ms
20,936 KB
testcase_39 AC 25 ms
9,956 KB
testcase_40 AC 58 ms
22,928 KB
testcase_41 AC 65 ms
25,072 KB
testcase_42 AC 53 ms
22,052 KB
testcase_43 AC 38 ms
15,456 KB
testcase_44 AC 41 ms
17,868 KB
testcase_45 AC 31 ms
14,860 KB
testcase_46 AC 46 ms
18,724 KB
testcase_47 AC 45 ms
17,348 KB
testcase_48 AC 62 ms
22,696 KB
testcase_49 AC 64 ms
24,420 KB
testcase_50 AC 13 ms
4,400 KB
testcase_51 AC 16 ms
4,416 KB
testcase_52 AC 10 ms
4,376 KB
testcase_53 AC 10 ms
4,380 KB
testcase_54 AC 9 ms
4,380 KB
testcase_55 AC 33 ms
14,904 KB
testcase_56 AC 15 ms
7,164 KB
testcase_57 AC 36 ms
17,472 KB
testcase_58 AC 46 ms
19,496 KB
testcase_59 AC 41 ms
17,464 KB
testcase_60 AC 33 ms
16,268 KB
testcase_61 AC 11 ms
5,332 KB
testcase_62 AC 44 ms
20,224 KB
testcase_63 AC 47 ms
21,524 KB
testcase_64 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=2e9+1;
const ll INF=4e18;
const ll dy[8]={-1,0,1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

vector< int > manacher(const string &s) {
  vector< int > radius(s.size());
  int i = 0, j = 0;
  while(i < s.size()) {
    while(i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) {
      ++j;
    }
    radius[i] = j;
    int k = 1;
    while(i - k >= 0 && i + k < s.size() && k + radius[i - k] < j) {
      radius[i + k] = radius[i - k];
      ++k;
    }
    i += k;
    j -= k;
  }
  return radius;
}

int main(){
  ll n,m;cin >> n >> m;
  string s;cin >> s;
  string t;cin >> t;
  if(m%2)cout << -1 << endl,exit(0);
  rep(i,t.size()){
    if(t[i]!=t[t.size()-1-i])cout << -1 << endl,exit(0);
  }
  t=t.substr(0,m/2);
  m/=2;
  vl rad(m);
  {
    string nt="$";
    rep(i,m){
      nt+=t[i];nt+="$";
    }
    auto man=manacher(nt);
    rep(i,m-1)rad[i]=man[i*2+2]/2;
  }
  //rep(i,m)cout << rad[i] <<" ";cout << endl;
  vl dist(m,inf);
  ll st=-1;
  {
    rep(i,s.size()){
      if(i>=t.size())break;
      if(s[i]!=t[i])break;
      chmax(st,i);
    }
    rev(all(s));
    rep(i,s.size()){
      if(i>=t.size())break;
      if(s[i]!=t[i])break;
      chmax(st,i);
    }
  }
  if(st==-1)cout << -1 << endl,exit(0);
  dist[st]=1;
  vvpl g(m);
  rep(i,m-1)g[i+1].push_back({i,0});
  rep(i,m-1){
    if(rad[i])g[i].push_back({i+rad[i],1});
  }
  deque<ll> que;que.push_back(st);
  while(!que.empty()){
    ll v=que.front();que.pop_front();
    for(auto p:g[v]){
      if(p.second==0){
        if(dist[p.first]>dist[v]){
          dist[p.first]=dist[v];
          que.push_front(p.first);
        }
      }
      else{
        if(dist[p.first]>dist[v]+1){
          dist[p.first]=dist[v]+1;
          que.push_back(p.first);
        }
      }
    }
  }
  //rep(i,m)cout << dist[i] <<" ";cout << endl;
  if(dist[m-1]>=inf)cout << -1 << endl;
  else cout << dist[m-1] << endl;
}
0