結果
問題 | No.422 文字列変更 (Hard) |
ユーザー | 0w1 |
提出日時 | 2016-12-05 18:13:35 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 239 ms / 3,000 ms |
コード長 | 3,907 bytes |
コンパイル時間 | 1,571 ms |
コンパイル使用メモリ | 179,132 KB |
実行使用メモリ | 307,584 KB |
最終ジャッジ日時 | 2024-05-05 21:14:46 |
合計ジャッジ時間 | 5,229 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
54,400 KB |
testcase_01 | AC | 185 ms
234,880 KB |
testcase_02 | AC | 201 ms
254,208 KB |
testcase_03 | AC | 179 ms
220,160 KB |
testcase_04 | AC | 191 ms
234,880 KB |
testcase_05 | AC | 208 ms
261,504 KB |
testcase_06 | AC | 234 ms
307,584 KB |
testcase_07 | AC | 37 ms
54,656 KB |
testcase_08 | AC | 46 ms
65,024 KB |
testcase_09 | AC | 234 ms
291,840 KB |
testcase_10 | AC | 239 ms
306,176 KB |
testcase_11 | AC | 185 ms
238,336 KB |
testcase_12 | AC | 173 ms
217,600 KB |
testcase_13 | AC | 179 ms
225,024 KB |
testcase_14 | AC | 182 ms
229,504 KB |
testcase_15 | AC | 188 ms
231,168 KB |
testcase_16 | AC | 188 ms
237,824 KB |
ソースコード
#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; }