結果

問題 No.170 スワップ文字列(Easy)
ユーザー nCk_cvnCk_cv
提出日時 2016-02-19 00:26:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 962 ms / 5,000 ms
コード長 960 bytes
コンパイル時間 1,962 ms
コンパイル使用メモリ 79,692 KB
実行使用メモリ 134,260 KB
最終ジャッジ日時 2024-06-02 06:23:53
合計ジャッジ時間 8,564 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
52,872 KB
testcase_01 AC 891 ms
132,732 KB
testcase_02 AC 106 ms
52,964 KB
testcase_03 AC 117 ms
53,968 KB
testcase_04 AC 152 ms
56,032 KB
testcase_05 AC 962 ms
133,968 KB
testcase_06 AC 861 ms
134,260 KB
testcase_07 AC 110 ms
52,964 KB
testcase_08 AC 122 ms
52,976 KB
testcase_09 AC 132 ms
54,156 KB
testcase_10 AC 573 ms
100,844 KB
testcase_11 AC 118 ms
54,140 KB
testcase_12 AC 106 ms
52,844 KB
testcase_13 AC 116 ms
54,148 KB
testcase_14 AC 114 ms
53,932 KB
testcase_15 AC 147 ms
56,036 KB
testcase_16 AC 117 ms
53,984 KB
testcase_17 AC 105 ms
53,104 KB
testcase_18 AC 107 ms
52,884 KB
testcase_19 AC 105 ms
52,972 KB
testcase_20 AC 118 ms
54,128 KB
testcase_21 AC 117 ms
54,044 KB
testcase_22 AC 120 ms
53,852 KB
testcase_23 AC 111 ms
53,504 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