CSES - Datatähti 2024 alku - Results
Submission details
Task:Monistus
Sender:EmuBird
Submission time:2023-10-30 13:53:03 +0200
Language:Java
Status:READY
Result:71
Feedback
groupverdictscore
#1ACCEPTED71
#20
Test results
testverdicttimegroup
#1ACCEPTED0.20 s1, 2details
#2ACCEPTED0.12 s1, 2details
#3ACCEPTED0.13 s1, 2details
#4ACCEPTED0.13 s1, 2details
#5ACCEPTED0.13 s1, 2details
#6ACCEPTED0.13 s1, 2details
#7ACCEPTED0.12 s1, 2details
#8--2details
#9--2details
#10--2details
#11--2details
#12--2details
#13--2details
#14--2details

Code

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

// Yes, this is a very odd solution.
public class Duplication {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String inputString = input.nextLine();
        input.close();

        DuplicationIterator iterator = new DuplicationIterator(new CharIterator(inputString));
        for (char character = iterator.next(); iterator.hasNext(); character = iterator.next()) {
            int digit = Character.digit(character, 10);
            if (digit == -1) {
                System.out.print(character);
            } else {
                iterator.duplicate(digit);
                iterator = new DuplicationIterator(iterator);
            }
        }
        System.out.println(); // Final newline
    }

    public static class DuplicationIterator implements Iterator<Character> {
        private final Iterator<Character> delegate;
        private final Queue<Character> repeatQueue;
        private int copiedChars = 0;

        public DuplicationIterator(Iterator<Character> delegate) {
            this.delegate = delegate;
            this.repeatQueue = new LinkedList<>();
        }

        public void duplicate(int n) {
            copiedChars = n;
        }

        @Override
        public boolean hasNext() {
            return delegate.hasNext() || !repeatQueue.isEmpty();
        }

        /**
         * @return next character or CharacterIterator.DONE
         */
        @Override
        public Character next() {
            if (copiedChars == 0 && !repeatQueue.isEmpty()) {
                return repeatQueue.remove();
            }

            char next = delegate.next();
            if (copiedChars > 0) {
                copiedChars--;
                repeatQueue.add(next);
                if (next == CharacterIterator.DONE) {
                    System.out.println("!!!");
                }
            }
            return next;
        }
    }

    public static class CharIterator implements Iterator<Character> {
        private final CharacterIterator delegate;
        private boolean isAtBeginning = true;
        private boolean isAtEnd = false;

        public CharIterator(String string) {
            this.delegate = new StringCharacterIterator(string);
        }

        @Override
        public boolean hasNext() {
            return !isAtEnd;
        }

        @Override
        public Character next() {
            if (isAtBeginning) {
                isAtBeginning = false;
                return delegate.first();
            } else {
                char next = delegate.next();
                if (next == CharacterIterator.DONE) {
                    isAtEnd = true;
                }
                return next;
            }
        }
    }
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
987654321o987654321p

correct output
oooooooooooooooooooooooooooooo...

user output
oooooooooooooooooooooooooooooo...
Truncated

Test 2

Group: 1, 2

Verdict: ACCEPTED

input
1e1o1zo1r1h1y1m1b1mn

correct output
eeoozzorrhhyymmbbmmn

user output
eeoozzorrhhyymmbbmmn

Test 3

Group: 1, 2

Verdict: ACCEPTED

input
h4y21n7dz6vr1p4go1ec

correct output
hynnnnynnnndzvrppdzvrppdzvrppg...

user output
hynnnnynnnndzvrppdzvrppdzvrppg...

Test 4

Group: 1, 2

Verdict: ACCEPTED

input
bv2olah1h6o4f2mx3z1k

correct output
bvololahhhofmxmxfmxmxofmxmxfmx...

user output
bvololahhhofmxmxfmxmxofmxmxfmx...

Test 5

Group: 1, 2

Verdict: ACCEPTED

input
xewpp2f1l6a5jp1v2v1y

correct output
xewppfffllajpvvajpvvajpvvjpjpv...

user output
xewppfffllajpvvajpvvajpvvjpjpv...

Test 6

Group: 1, 2

Verdict: ACCEPTED

input
1v4z1kjm1t6vp5c1m2np

correct output
vvzkkjzkkjmttvpcmmvpcmmvpcmmnc...

user output
vvzkkjzkkjmttvpcmmvpcmmvpcmmnc...

Test 7

Group: 1, 2

Verdict: ACCEPTED

input
y2bl9cp21v7kgesxm2uv

correct output
yblblcpvvvvkgecpkkkkgecpvvvvkg...

user output
yblblcpvvvvkgecpkkkkgecpvvvvkg...

Test 8

Group: 2

Verdict:

input
987654321b987654321f987654321u...

correct output
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...

user output
(empty)

Test 9

Group: 2

Verdict:

input
agjv4321w9dskvax5ws21r321l9n7d...

correct output
agjvwwwwwwwwwwwwwwwwdskvaxwsds...

user output
(empty)

Test 10

Group: 2

Verdict:

input
4mr1d71k432jt9sddyoy2oq7jvc1bm...

correct output
mrddmrddkkjtjtjtjtjtjtjtjtkkjt...

user output
(empty)

Test 11

Group: 2

Verdict:

input
1a1l1m1a1t1gc1yv1x1p1w1x1d1d1p...

correct output
aallmmaattggcyyvxxppwwxxddddpp...

user output
(empty)

Test 12

Group: 2

Verdict:

input
321e321b321a321r321m321f321s32...

correct output
eeeeeeeebbbbbbbbaaaaaaaarrrrrr...

user output
(empty)

Test 13

Group: 2

Verdict:

input
4321e4321l4321t4321w4321g4321c...

correct output
eeeeeeeeeeeeeeeellllllllllllll...

user output
(empty)

Test 14

Group: 2

Verdict:

input
987654321u987654321p987654321g...

correct output
uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu...

user output
(empty)