結果

問題 No.170 スワップ文字列(Easy)
ユーザー nCk_cvnCk_cv
提出日時 2016-02-19 00:26:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,140 ms / 5,000 ms
コード長 960 bytes
コンパイル時間 2,367 ms
コンパイル使用メモリ 76,812 KB
実行使用メモリ 138,244 KB
最終ジャッジ日時 2023-08-24 16:27:57
合計ジャッジ時間 9,935 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,624 KB
testcase_01 AC 1,006 ms
137,468 KB
testcase_02 AC 116 ms
55,752 KB
testcase_03 AC 120 ms
55,852 KB
testcase_04 AC 180 ms
58,376 KB
testcase_05 AC 1,015 ms
138,244 KB
testcase_06 AC 1,140 ms
137,708 KB
testcase_07 AC 120 ms
56,148 KB
testcase_08 AC 133 ms
55,744 KB
testcase_09 AC 135 ms
56,084 KB
testcase_10 AC 726 ms
103,692 KB
testcase_11 AC 116 ms
55,980 KB
testcase_12 AC 122 ms
56,116 KB
testcase_13 AC 121 ms
55,788 KB
testcase_14 AC 117 ms
55,764 KB
testcase_15 AC 161 ms
57,800 KB
testcase_16 AC 116 ms
56,032 KB
testcase_17 AC 121 ms
56,272 KB
testcase_18 AC 121 ms
56,208 KB
testcase_19 AC 115 ms
56,036 KB
testcase_20 AC 116 ms
55,896 KB
testcase_21 AC 114 ms
55,696 KB
testcase_22 AC 123 ms
55,492 KB
testcase_23 AC 118 ms
55,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.awt.geom.*;
import java.io.*;

class Main {
	static HashMap<String,Boolean> map = new HashMap<String,Boolean>();
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String in = sc.next();
		StringBuilder a = new StringBuilder(in);
		dfs(a);
		System.out.println(map.size()-1);
	}
	
	static void dfs(StringBuilder a) {
		ArrayDeque<StringBuilder> queue = new ArrayDeque<StringBuilder>();
		queue.add(new StringBuilder(a));
		while(!queue.isEmpty()) {
			a = queue.pollFirst();
			String t = a.toString();
			if(map.containsKey(a.toString())) continue;
			map.put(a.toString(), true);
			int tmp = a.length();
			for(int i = 0; i < tmp; i++) {
				char b = a.charAt(i);
				for(int j = i+1; j < tmp; j++) {
					a = a.replace(i, i+1, String.valueOf(a.charAt(j)));
					a = a.replace(j, j+1, String.valueOf(b));
					queue.add(new StringBuilder(a));
					a = new StringBuilder(t);
				}
			}
		}
	}
}
0