結果

問題 No.170 スワップ文字列(Easy)
ユーザー ccppjsrbccppjsrb
提出日時 2020-07-17 12:20:00
言語 Go
(1.22.1)
結果
AC  
実行時間 49 ms / 5,000 ms
コード長 1,859 bytes
コンパイル時間 16,054 ms
コンパイル使用メモリ 213,848 KB
実行使用メモリ 8,816 KB
最終ジャッジ日時 2023-08-18 18:10:08
合計ジャッジ時間 12,241 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 47 ms
8,816 KB
testcase_02 AC 37 ms
7,868 KB
testcase_03 AC 37 ms
7,876 KB
testcase_04 AC 38 ms
7,880 KB
testcase_05 AC 47 ms
8,812 KB
testcase_06 AC 47 ms
8,752 KB
testcase_07 AC 6 ms
5,500 KB
testcase_08 AC 39 ms
7,880 KB
testcase_09 AC 37 ms
7,876 KB
testcase_10 AC 49 ms
8,064 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	s := getNextString(scanner)
	n := len(s)
	sm := stringmap{}
	permutation(0, n, 0, s, make([]int, n), sm.swapped)
	fmt.Fprintln(writer, len(sm)-1)
}

func permutation(i, n int, used uint32, s string, a []int, f func(ss string, aa []int)) {
	if i == n {
		f(s, a)
	}
	for ii := 0; ii < n; ii++ {
		if used>>uint(ii)&1 == 1 {
			continue
		}
		a[i] = ii
		permutation(i+1, n, used|1<<uint(ii), s, a, f)
	}
}

type stringmap map[string]bool

func (sm *stringmap) swapped(s string, a []int) {
	n := len(s)
	ss := make([]string, n)
	for i := 0; i < n; i++ {
		ss[i] = fmt.Sprintf("%c", s[a[i]])
	}
	(*sm)[strings.Join(ss, "")] = true
}
0