結果
問題 | No.422 文字列変更 (Hard) |
ユーザー | 0w1 |
提出日時 | 2016-12-05 17:33:43 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,899 bytes |
コンパイル時間 | 1,619 ms |
コンパイル使用メモリ | 179,800 KB |
実行使用メモリ | 307,712 KB |
最終ジャッジ日時 | 2024-05-05 21:14:36 |
合計ジャッジ時間 | 5,874 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 46 ms
64,896 KB |
testcase_09 | AC | 286 ms
291,968 KB |
testcase_10 | AC | 292 ms
306,176 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
ソースコード
#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( S[ a - 1 ] ); break; case 1: s1.emplace( S[ a - 1 ] ), s2.emplace( S[ a - 1 ] ); break; case 2: s1.emplace( '-' ); break; case 3: s1.emplace( '-' ), s2.emplace( S[ a - 1 ] ); break; case 4: s1.emplace( S[ b - 1 ] ); break; case 5: s1.emplace( '-' ), s2.emplace( '-' ); break; } } string str1, str2; for( ; not s1.empty(); s1.pop(), s2.pop() ) str1 += s1.top(), str2 += s2.top(); cout << str2 << endl; cout << str1 << endl; exit( 0 ); } } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }