結果

問題 No.225 文字列変更(medium)
ユーザー wahr69wahr69
提出日時 2015-08-05 14:19:14
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,382 bytes
コンパイル時間 3,587 ms
コンパイル使用メモリ 75,032 KB
実行使用メモリ 62,816 KB
最終ジャッジ日時 2023-09-25 03:59:13
合計ジャッジ時間 16,124 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.LinkedList;
import java.util.List;

public class no225 {
	
	static int N;
	static int M;
	static String S;
	static String T;
	
	// 終了したらfalseを返す
	public static boolean next(int[] indexes) {
		int i = indexes.length - 1;
		indexes[i] += 1;
		int count = 0;
		while (indexes[i] >= N - count) {
			if (i == 0) {
				return false;
			}
			indexes[i - 1] += 1;
			--i;
			++count;
		}
		for (int j = i + 1; j < indexes.length; ++j) {
			indexes[j] = indexes[j - 1] + 1;
		}
		return true;
	}
	
	public static boolean isCorrect(List<Character> a, String b) {
		int count = 0;
		for (Character c : a) { 
			if (!c.equals(b.charAt(count))) {
				return false;
			}
			++count;
		}
		return true;
	}
	
	public static int illiegalCharsCount(List<Character> a, String b) {
		int count = 0;
		int ret = 0;
		for (Character c : a) { 
			if (!c.equals(b.charAt(count))) {
				++ret;
			}
			++count;
		}
		return ret;
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		String[] input = r.readLine().split(" ");
		N = Integer.parseInt(input[0]);
		M = Integer.parseInt(input[1]);
		S = r.readLine();
		T = r.readLine();
		
		String newS = S;
		if (M > N) {
			int temp = N;
			N = M;
			M = temp;
			
			String tempS = S;
			S = T;
			T = tempS;
		}
		
		if (N > M) {
			// 多い時、削除する
			int[] indexes = new int[N - M];
			for (int i = 0; i < N - M; ++i) {
				indexes[i] = i;
			}
			int min = 99999999;
			String minStr = "";
			boolean ret = true;
			while (ret) {
				List<Character> newStr = new LinkedList<Character>();
				int index = 0;
				for (int i = 0; i < N; ++i) {
					if (index < indexes.length && i == indexes[index]) {
						++index;
						continue;
					}
					newStr.add(S.charAt(i));
				}
				
				int illiegalCharsNum = illiegalCharsCount(newStr, T);
				if (illiegalCharsNum < min) {
					min = illiegalCharsNum;
					minStr = "";
					for (Character c : newStr) {
						minStr += c;
					}
				}
				
				ret = next(indexes);
			}
			
			newS = minStr;
		}
		
		// 文字列変更カウント
		int result = 0;
		for (int i = 0; i < M; ++i) {
			if (newS.charAt(i) != T.charAt(i)) {
				++result;
			}
		}
		
		System.out.println(result + Math.abs(N - M));
	}
	
}
0