CSES - Datatähti 2022 alku - Results
Submission details
Task:Ositus (Partitioning)
Sender:Mixu_78
Submission time:2021-10-05 17:24:29
Language:Node.js
Status:READY
Result:40
Feedback
groupverdictscore
#1ACCEPTED40
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.44 s1, 2, 3details
#2ACCEPTED0.46 s1, 2, 3details
#3ACCEPTED0.46 s1, 2, 3details
#4ACCEPTED0.46 s1, 2, 3details
#5--2, 3details
#60.44 s3details
#70.45 s3details

Code

const fs = require("fs");
const readline = require("readline");

const str = fs.existsSync("./subIn.txt") ? fs.readFileSync("./subIn.txt").toString() : "";

const stdin = readline.createInterface({
	input: process.stdin,
});

/**
 * @param {string} str
 * @returns {Generator<string[]>}
 */
function* partitions(str) {
	if (str.length > 0) {
		for (let i = 1; i < str.length + 1; i++) {
			const a = str.slice(0, i);
			const rest = str.slice(i);
			for (const p of partitions(rest)) {
				yield [a, ...p];
			}
		}
	} else {
		yield [];
	}
}

/**@param {string} s */
const hasDuplicate = (s) => {
	for (let i = 0; i < s.length; i++) {
		if (s.lastIndexOf(s[i]) !== i) return true;
	}
	return false;
}

const getTotal = (s) => {
	if (s.length > 0) {
		let total = 0;
		for (const part of partitions(s)) {
			const ok = part.every((s) => !hasDuplicate(s));
			if (ok) total++;
		}
	
		return total;
	}
}

if (str.length > 0) {
	console.log(getTotal(str));
	stdin.close();
} else {
	stdin.on("line", (l) => {
		console.log(getTotal(l))
		stdin.close();
	});
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
a

correct output
1

user output
1

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
abcdefghij

correct output
512

user output
512

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
abcabaacbc

correct output
120

user output
120

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
aaxxxxxxaa

correct output
4

user output
4

Test 5

Group: 2, 3

Verdict:

input
mfyzvoxmppoxcvktmcjkryyocfweub...

correct output
643221148

user output
(empty)

Test 6

Group: 3

Verdict:

input
weinscqmmpgbrlboocvtbptgbahmwv...

correct output
831644159

user output
(empty)

Error:
/box/input/code.js:1
(function (exports, require, module, __filename, __dirname) { const fs = require("fs");
^

RangeError: Maximum call stack size exceeded
    at partitions (/box/input/code.js:1:1)
    at partitions (/box/input/code.js:19:20)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)

Test 7

Group: 3

Verdict:

input
sxaoxcyrjoeieyinaqxwukgzdnhhsw...

correct output
816016015

user output
(empty)

Error:
/box/input/code.js:1
(function (exports, require, module, __filename, __dirname) { const fs = require("fs");
^

RangeError: Maximum call stack size exceeded
    at partitions (/box/input/code.js:1:1)
    at partitions (/box/input/code.js:19:20)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)
    at partitions.next (<anonymous>)
    at partitions (/box/input/code.js:19:15)