結果

問題 No.422 文字列変更 (Hard)
ユーザー hermione17hermione17
提出日時 2016-09-10 00:38:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,529 ms / 3,000 ms
コード長 3,380 bytes
コンパイル時間 3,380 ms
コンパイル使用メモリ 74,608 KB
実行使用メモリ 361,672 KB
最終ジャッジ日時 2023-08-10 21:06:23
合計ジャッジ時間 21,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,040 KB
testcase_01 AC 1,058 ms
280,224 KB
testcase_02 AC 1,219 ms
289,604 KB
testcase_03 AC 1,139 ms
278,276 KB
testcase_04 AC 1,053 ms
279,952 KB
testcase_05 AC 1,099 ms
293,356 KB
testcase_06 AC 1,529 ms
361,672 KB
testcase_07 AC 130 ms
56,580 KB
testcase_08 AC 138 ms
57,568 KB
testcase_09 AC 1,212 ms
337,936 KB
testcase_10 AC 1,247 ms
346,920 KB
testcase_11 AC 1,069 ms
284,980 KB
testcase_12 AC 1,226 ms
275,600 KB
testcase_13 AC 1,048 ms
279,676 KB
testcase_14 AC 1,054 ms
282,444 KB
testcase_15 AC 1,064 ms
282,536 KB
testcase_16 AC 1,055 ms
285,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;


public class Y422 {
	Y422() throws IOException {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int m = sc.nextInt();
		char[] S = sc.next().toCharArray();
		char[] T = sc.next().toCharArray();

		int[][][] dp = new int[n+1][m+1][3];
		int[][][][] from = new int[n+1][m+1][3][];

		for (int i = 0; i <= S.length; i++) {
			for (int j = 0; j <= T.length; j++) {
				for (int a = 0; a < 3; a ++) {
					dp[i][j][a] = 100 * (n + m);
				}
			}
		}
		
		dp[0][0][0] = 0;
				
		
		for (int i = -1; i < S.length; i++) {
			for (int j = -1; j < T.length; j++) {
						
				int cost;
				if (i >= 0 && j >= 0) {
					int change_cost = 5;
					if (S[i] == T[j]) change_cost = 0;

					cost = change_cost + dp[i][j][0];
					{
						int[] tmp = { i, j, 0 };
						dp[i+1][j+1][0] = cost;
						from[i+1][j+1][0] = tmp;
					}

					cost = change_cost + dp[i][j][1];
					if (cost < dp[i+1][j+1][0]) {
						int[] tmp = { i, j, 1 };
						dp[i+1][j+1][0] = cost;
						from[i+1][j+1][0] = tmp;
					}

					cost = change_cost + dp[i][j][2];
					if (cost < dp[i+1][j+1][0]) {
						int[] tmp = { i, j, 2 };
						dp[i+1][j+1][0] = cost;
						from[i+1][j+1][0] = tmp;
					}
				}				
				
				if (j >= 0) {
					cost = 9 + dp[i+1][j][0];
					{
						int[] tmp = { i+1, j, 0 };
						dp[i+1][j+1][2] = cost;
						from[i+1][j+1][2] = tmp;
					}

					cost = 2 + dp[i+1][j][2];
					if (cost < dp[i+1][j+1][2]) {
						int[] tmp = { i+1, j, 2 };
						dp[i+1][j+1][2] = cost;
						from[i+1][j+1][2] = tmp;
					}

					cost = 9 + dp[i+1][j][1];
					if (cost < dp[i+1][j+1][2]) {
						int[] tmp = { i+1, j, 1 };
						dp[i+1][j+1][2] = cost;
						from[i+1][j+1][2] = tmp;
					}
				}
				
				
				if (i >= 0) {
					cost = 9 + dp[i][j+1][0];
					{
						int[] tmp = { i, j+1,0 };
						dp[i+1][j+1][1] = cost;
						from[i+1][j+1][1] = tmp;
					}

					cost = 9 + dp[i][j+1][2];
					if (cost < dp[i+1][j+1][1]) {
						int[] tmp = { i, j+1, 2 };
						dp[i+1][j+1][1] = cost;
						from[i+1][j+1][1] = tmp;
					}

					cost = 2 + dp[i][j+1][1];
					if (cost < dp[i+1][j+1][1]) {
						int[] tmp = { i, j+1, 1 };
						dp[i+1][j+1][1] = cost;
						from[i+1][j+1][1] = tmp;
					}
				}
			}
		}
		
		
		int cost = 100 * (n + m);
		int[] state = null;
		for (int a = 0; a < 3; a++) {
			if (cost > dp[S.length][T.length][a]) {
				cost = dp[S.length][T.length][a];
				int[] tmp = {S.length, T.length, a};
				state = tmp;
			}
		}
		
		
		char[] S_ans = new char[n+m];
		char[] T_ans = new char[n+m];
		int len = 0;
		
		while (state[0] != 0 || state[1] != 0) {
			int[] next = from[state[0]][state[1]][state[2]];

			
			if (state[2] == 2) {
				S_ans[len] = '-';
			} else {
				S_ans[len] = S[state[0]-1];
			}
			if (state[2] == 1) {
				T_ans[len] = '-';
			} else {
				T_ans[len] = T[state[1]-1];
			}
			len++;
			state = next;
		}

		OutputStream os = new BufferedOutputStream(System.out);
		
		os.write(Integer.toString(cost).getBytes());
		os.write('\n');

		for (int i = len - 1; i >= 0; i--) {
			os.write(S_ans[i]);
		}
		os.write('\n');

		for (int i = len - 1; i >= 0; i--) {
			os.write(T_ans[i]);
		}
		os.write('\n');
	
		os.flush();
	}
	public static void main(String argv[]) throws IOException {
		new Y422();
	}
}
0