結果

問題 No.422 文字列変更 (Hard)
ユーザー 0w10w1
提出日時 2016-12-05 18:13:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 3,000 ms
コード長 3,907 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 177,404 KB
実行使用メモリ 307,640 KB
最終ジャッジ日時 2023-08-18 16:08:08
合計ジャッジ時間 4,509 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
62,120 KB
testcase_01 AC 134 ms
245,372 KB
testcase_02 AC 144 ms
260,184 KB
testcase_03 AC 126 ms
234,784 KB
testcase_04 AC 134 ms
250,328 KB
testcase_05 AC 148 ms
266,252 KB
testcase_06 AC 172 ms
307,640 KB
testcase_07 AC 15 ms
57,856 KB
testcase_08 AC 36 ms
149,068 KB
testcase_09 AC 164 ms
296,428 KB
testcase_10 AC 182 ms
306,880 KB
testcase_11 AC 134 ms
252,424 KB
testcase_12 AC 125 ms
239,916 KB
testcase_13 AC 132 ms
244,384 KB
testcase_14 AC 131 ms
247,048 KB
testcase_15 AC 133 ms
248,108 KB
testcase_16 AC 135 ms
252,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef vector< pii > vp;
typedef vector< vp > vvp;
typedef vector< string > vs;
typedef vector< double > vd;
typedef vector< vd > vvd;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N, M;
string S;
string T;

void init(){
  cin >> N >> M;
  cin >> S;
  cin >> T;
}

int dp[ 1200 + 1 ][ 1200 + 1 ][ 3 ][ 3 ];
tuple< int, int, int, int > pre[ 1200 + 1 ][ 1200 + 1 ][ 3 ][ 3 ];
int ds[ 1200 + 1 ][ 1200 + 1 ][ 3 ][ 3 ];

void preprocess(){
  memset( dp, INF, sizeof( dp ) );
  dp[ 0 ][ 0 ][ 0 ][ 0 ] = 0;
  for( int i = 0; i <= N; ++i )
    for( int j = 0; j <= M; ++j )
      for( int t1 = 0; t1 < 3; ++t1 )
        for( int t2 = 0; t2 < 3; ++t2 )
          if( dp[ i ][ j ][ t1 ][ t2 ] < INF ){
            if( i < N and j < M and S[ i ] == T[ j ] ){
              if( upmin( dp[ i + 1 ][ j + 1 ][ 0 ][ 0 ], dp[ i ][ j ][ t1 ][ t2 ] ) )
                pre[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = tie( i, j, t1, t2 ),
                ds[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = 0;
            }
            if( i + 1 <= N and j + 1 <= M ){
              if( upmin( dp[ i + 1 ][ j + 1 ][ 0 ][ 0 ], dp[ i ][ j ][ t1 ][ t2 ] + 5 ) )
                pre[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = tie( i, j, t1, t2 ),
                ds[ i + 1 ][ j + 1 ][ 0 ][ 0 ] = 1;
            }
            if( i + 1 <= N ){/*
              if( upmin( dp[ i + 1 ][ j ][ 1 ][ t2 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t1 != 1 ? 9 : 2 ) ) )
                pre[ i + 1 ][ j ][ 1 ][ t2 ] = tie( i, j, t1, t2 ),
                ds[ i + 1 ][ j ][ 1 ][ t2 ] = 2;*/
              if( upmin( dp[ i + 1 ][ j ][ t1 ][ 2 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t2 != 2 ? 9 : 2 ) ) )
                pre[ i + 1 ][ j ][ t1 ][ 2 ] = tie( i, j, t1, t2 ),
                ds[ i + 1 ][ j ][ t1 ][ 2 ] = 3;
            }
            if( j + 1 <= M ){
              if( upmin( dp[ i ][ j + 1 ][ 2 ][ t2 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t1 != 2 ? 9 : 2 ) ) )
                pre[ i ][ j + 1 ][ 2 ][ t2 ] = tie( i, j, t1, t2 ),
                ds[ i ][ j + 1 ][ 2 ][ t2 ] = 4;/*
              if( upmin( dp[ i ][ j + 1 ][ t1 ][ 1 ], dp[ i ][ j ][ t1 ][ t2 ] + ( t2 != 1 ? 9 : 2 ) ) )
                pre[ i ][ j + 1 ][ t1 ][ 1 ] = tie( i, j, t1, t2 ),
                ds[ i ][ j + 1 ][ t1 ][ 1 ] = 5;*/
            }
          }
}

void solve(){
  int min_cost = INF;
  for( int i = 0; i < 3; ++i )
    for( int j = 0; j < 3; ++j )
      upmin( min_cost, dp[ N ][ M ][ i ][ j ] );
  cout << min_cost << endl;
  for( int i = 0; i < 3; ++i )
    for( int j = 0; j < 3; ++j )
      if( min_cost == dp[ N ][ M ][ i ][ j ] ){
        stack< char > s1, s2;
        for( int a = N, b = M, c = i, d = j; not ( a == 0 and b == 0 ); tie( a, b, c, d ) = pre[ a ][ b ][ c ][ d ] ){
          int op = ds[ a ][ b ][ c ][ d ];
          switch( op ){
            case 0: s1.emplace( S[ a - 1 ] ), s2.emplace( T[ b - 1 ] ); break;
            case 1: s1.emplace( S[ a - 1 ] ), s2.emplace( T[ b - 1 ] ); break;
            case 2: s1.emplace( '-' ); break;
            case 3: s1.emplace( S[ a - 1 ] ), s2.emplace( '-' ); break;
            case 4: s1.emplace( '-' ), s2.emplace( T[ b - 1 ] ); break;
            case 5: s2.emplace( '-' ); break;
          }
        }
        string str1, str2;
        for( ; not s1.empty(); s1.pop(), s2.pop() )
          str1 += s1.top(),
          str2 += s2.top();
        cout << str1 << endl;
        cout << str2 << endl;
        exit( 0 ); 
      }
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0