CSES - Datatähti 2016 alku - Results
Submission details
Task:Bittipeli
Sender:rxz9000
Submission time:2015-10-11 00:55:26
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.17 s1details
#2ACCEPTED0.17 s1details
#3ACCEPTED0.17 s1details
#40.17 s1details
#5ACCEPTED0.17 s1details
#6ACCEPTED0.17 s1details
#7ACCEPTED0.17 s1details
#8ACCEPTED0.17 s1details
#9ACCEPTED0.17 s1details
#10ACCEPTED0.17 s1details
#11ACCEPTED0.17 s1details
#12ACCEPTED0.17 s1details
#13ACCEPTED0.17 s1details
#14ACCEPTED0.18 s1details
#15ACCEPTED0.17 s1details
#16ACCEPTED0.17 s1details
#17ACCEPTED0.16 s1details
#18ACCEPTED0.18 s1details
#190.18 s1details
#20ACCEPTED0.18 s1details
#21ACCEPTED0.17 s2details
#22ACCEPTED0.17 s2details
#23ACCEPTED0.17 s2details
#24ACCEPTED0.17 s2details
#25ACCEPTED0.18 s2details
#260.16 s2details
#270.18 s2details
#28ACCEPTED0.17 s2details
#29ACCEPTED0.17 s2details
#30ACCEPTED0.17 s2details
#31ACCEPTED0.19 s2details
#32ACCEPTED0.17 s2details
#33ACCEPTED0.18 s2details
#340.17 s2details
#35ACCEPTED0.17 s2details
#36ACCEPTED0.17 s2details
#37ACCEPTED0.17 s2details
#380.17 s2details
#39ACCEPTED0.19 s2details
#400.17 s2details
#41ACCEPTED0.17 s3details
#420.18 s3details
#430.19 s3details
#44ACCEPTED0.18 s3details
#450.17 s3details
#46ACCEPTED0.17 s3details
#47ACCEPTED0.17 s3details
#48ACCEPTED0.17 s3details
#490.17 s3details
#50ACCEPTED0.18 s3details
#510.18 s3details
#52ACCEPTED0.18 s3details
#53ACCEPTED0.17 s3details
#540.17 s3details
#55ACCEPTED0.19 s3details
#56ACCEPTED0.19 s3details
#57ACCEPTED0.18 s3details
#580.19 s3details
#59ACCEPTED0.19 s3details
#600.18 s3details
#61ACCEPTED0.18 s4details
#62ACCEPTED0.17 s4details
#63ACCEPTED0.18 s4details
#640.18 s4details
#65ACCEPTED0.18 s4details
#66ACCEPTED0.17 s4details
#67ACCEPTED0.17 s4details
#68ACCEPTED0.17 s4details
#69ACCEPTED0.17 s4details
#700.17 s4details
#71ACCEPTED0.17 s4details
#72ACCEPTED0.18 s4details
#73ACCEPTED0.18 s4details
#74ACCEPTED0.17 s4details
#75ACCEPTED0.18 s4details
#76ACCEPTED0.27 s4details
#77ACCEPTED0.27 s4details
#780.24 s4details
#79--4details
#80ACCEPTED0.78 s4details

Code

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class BittipeliTehtava {

    private static int fillList(List sequences, String str) {
        int nOfLongSequences = 0;
        
        int lengthMinusOne = str.length() - 1;
        for (int i = 0; i <= lengthMinusOne; i++) {
            if (i == lengthMinusOne || str.charAt(i) != str.charAt(i + 1)) {
                if (i > 0 && str.charAt(i) == str.charAt(i - 1)) {
                    sequences.add(new Sequence(str.charAt(i), true));
                    nOfLongSequences++;
                } else {
                    sequences.add(new Sequence(str.charAt(i), false));
                }
            }
        }
        
        return nOfLongSequences;
    }

    private static int removeSequences(List<Sequence> sequences) {
        int amountOfMoves = 0;
        Stack<Sequence> stackOfSequences = new Stack<>();

        int length = sequences.size();

        for (int i = 0; i < length; i++) {
            Sequence currentSequence = sequences.get(i);
            if (!stackOfSequences.isEmpty() && stackOfSequences.peek().token == currentSequence.token) {
                stackOfSequences.pop();
                amountOfMoves++;
            } else if (currentSequence.isLongerThan1) {
                amountOfMoves++;
            } else {
                stackOfSequences.push(currentSequence);
            }
        }

        if (stackOfSequences.isEmpty()) {
            return amountOfMoves;
        }
        return -1;
    }
    
    private static List<Integer> removeSequencesBackwards(List<Sequence> sequences, int nOfLongSequences) {
        List<Integer> moves = new LinkedList<>();
        Stack<Sequence> stackOfSequences = new Stack<>();
        
        int currentLongSequence = nOfLongSequences;

        for (int i = sequences.size() - 1; i >= 0; i--) {
            Sequence currentSequence = sequences.get(i);
            if (!stackOfSequences.isEmpty() && stackOfSequences.peek().token == currentSequence.token) {
                moves.add(currentLongSequence);         
                stackOfSequences.pop();
                if (stackOfSequences.isEmpty() || (i >= 1 && sequences.get(i - 1).isLongerThan1)) {
                    currentLongSequence--;
                }
            } else if (currentSequence.isLongerThan1) {
                moves.add(currentLongSequence);
                if (stackOfSequences.isEmpty() || (i >= 1 && sequences.get(i - 1).isLongerThan1)) {
                    currentLongSequence--;
                }
            } else {
                stackOfSequences.push(currentSequence);
            }
        }       
        if (stackOfSequences.isEmpty()) {
            return moves;
        }
        return null;
    }

    public static void main(String[] args) {

        IO io = new IO();
        String str = io.next();

        List<Sequence> sequences = new ArrayList<>();
        int nOfLongSequences = fillList(sequences, str);
        
        int moves1 = removeSequences(sequences);
        List<Integer> moves2 = removeSequencesBackwards(sequences, nOfLongSequences);
        
        StringBuilder output = new StringBuilder();
        
        if (moves1 > 0) {
            output.append("1");
            for (int i = 1; i < moves1; i++) {
                output.append(" 1");
            }
            io.println(moves1);
            io.println(output.toString());
        } else if (moves2 != null) {           
            output.append(moves2.get(0));
            int length = moves2.size();
            for (int i = 1; i < length; i++) {
                output.append(" ");
                output.append(moves2.get(i));
            }
            io.println(moves2.size());
            io.println(output.toString());
        } else {
            io.println("QAQ");
        }

        io.close();
    }
}

class Sequence {

    public final char token;
    public final boolean isLongerThan1;

    public Sequence(char token, boolean longerThan1) {
        this.token = token;
        this.isLongerThan1 = longerThan1;
    }
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
1011001010

correct output
QAQ

user output
QAQ

Test 2

Group: 1

Verdict: ACCEPTED

input
0000001001

correct output
3
1 1 1 

user output
3
1 1 1

Test 3

Group: 1

Verdict: ACCEPTED

input
0111011100

correct output
3
2 1 1 

user output
4
1 1 1 1

Test 4

Group: 1

Verdict:

input
0100100100

correct output
4
2 1 1 1 

user output
QAQ

Test 5

Group: 1

Verdict: ACCEPTED

input
1110010110

correct output
4
2 1 1 1 

user output
4
3 3 2 1

Test 6

Group: 1

Verdict: ACCEPTED

input
1111110110

correct output
3
1 1 1 

user output
3
1 1 1

Test 7

Group: 1

Verdict: ACCEPTED

input
0011110001

correct output
3
1 2 1 

user output
3
3 2 1

Test 8

Group: 1

Verdict: ACCEPTED

input
0111111000

correct output
2
1 1 

user output
2
1 1

Test 9

Group: 1

Verdict: ACCEPTED

input
1111111100

correct output
2
1 1 

user output
2
1 1

Test 10

Group: 1

Verdict: ACCEPTED

input
1000010011

correct output
3
2 1 1 

user output
4
1 1 1 1

Test 11

Group: 1

Verdict: ACCEPTED

input
1101110000

correct output
3
1 1 1 

user output
3
1 1 1

Test 12

Group: 1

Verdict: ACCEPTED

input
1101101100

correct output
4
1 2 1 1 

user output
5
1 1 1 1 1

Test 13

Group: 1

Verdict: ACCEPTED

input
0100111110

correct output
3
1 1 1 

user output
3
1 1 1

Test 14

Group: 1

Verdict: ACCEPTED

input
1101001011

correct output
4
2 2 2 1 

user output
5
1 1 1 1 1

Test 15

Group: 1

Verdict: ACCEPTED

input
1110110010

correct output
4
1 2 1 1 

user output
4
3 2 2 1

Test 16

Group: 1

Verdict: ACCEPTED

input
0011011100

correct output
3
3 2 1 

user output
4
1 1 1 1

Test 17

Group: 1

Verdict: ACCEPTED

input
1100101011

correct output
QAQ

user output
QAQ

Test 18

Group: 1

Verdict: ACCEPTED

input
1101100111

correct output
3
2 2 1 

user output
4
1 1 1 1

Test 19

Group: 1

Verdict:

input
0110000100

correct output
3
2 1 1 

user output
QAQ

Test 20

Group: 1

Verdict: ACCEPTED

input
0000101000

correct output
QAQ

user output
QAQ

Test 21

Group: 2

Verdict: ACCEPTED

input
1110010100

correct output
QAQ

user output
QAQ

Test 22

Group: 2

Verdict: ACCEPTED

input
1110010000

correct output
3
2 1 1 

user output
3
3 2 1

Test 23

Group: 2

Verdict: ACCEPTED

input
1001101100

correct output
4
1 1 1 1 

user output
4
1 1 1 1

Test 24

Group: 2

Verdict: ACCEPTED

input
0000000111

correct output
2
1 1 

user output
2
1 1

Test 25

Group: 2

Verdict: ACCEPTED

input
0011111110

correct output
2
2 1 

user output
2
2 1

Test 26

Group: 2

Verdict:

input
1100101100

correct output
4
2 1 1 1 

user output
QAQ

Test 27

Group: 2

Verdict:

input
0111101110

correct output
3
2 1 1 

user output
QAQ

Test 28

Group: 2

Verdict: ACCEPTED

input
0000011011

correct output
3
2 1 1 

user output
3
3 2 1

Test 29

Group: 2

Verdict: ACCEPTED

input
1110011101

correct output
3
3 2 1 

user output
3
3 2 1

Test 30

Group: 2

Verdict: ACCEPTED

input
0001011011

correct output
4
1 1 1 1 

user output
4
1 1 1 1

Test 31

Group: 2

Verdict: ACCEPTED

input
0010110011

correct output
4
1 1 1 1 

user output
4
1 1 1 1

Test 32

Group: 2

Verdict: ACCEPTED

input
0011100000

correct output
2
2 1 

user output
3
1 1 1

Test 33

Group: 2

Verdict: ACCEPTED

input
1110100110

correct output
4
1 1 1 1 

user output
4
1 1 1 1

Test 34

Group: 2

Verdict:

input
0110110111

correct output
4
2 1 1 1 

user output
QAQ

Test 35

Group: 2

Verdict: ACCEPTED

input
1110110010

correct output
4
1 2 1 1 

user output
4
3 2 2 1

Test 36

Group: 2

Verdict: ACCEPTED

input
110011001100110011001010101010...

correct output
QAQ

user output
QAQ

Test 37

Group: 2

Verdict: ACCEPTED

input
110011001100110011001100101010...

correct output
QAQ

user output
QAQ

Test 38

Group: 2

Verdict:

input
110011001100110011001100110011...

correct output
31
1 20 20 20 20 20 20 20 20 20 2...

user output
QAQ

Test 39

Group: 2

Verdict: ACCEPTED

input
010101010101010101010101010101...

correct output
48
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
48
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 40

Group: 2

Verdict:

input
011010000011111011110000110011...

correct output
23
13 13 12 11 11 11 11 10 9 9 9 ...

user output
QAQ

Test 41

Group: 3

Verdict: ACCEPTED

input
0010101000

correct output
QAQ

user output
QAQ

Test 42

Group: 3

Verdict:

input
0100010110

correct output
4
2 1 1 1 

user output
QAQ

Test 43

Group: 3

Verdict:

input
0100110110

correct output
4
2 1 1 1 

user output
QAQ

Test 44

Group: 3

Verdict: ACCEPTED

input
1110000001

correct output
2
2 1 

user output
2
2 1

Test 45

Group: 3

Verdict:

input
0001001110

correct output
3
2 2 1 

user output
QAQ

Test 46

Group: 3

Verdict: ACCEPTED

input
0011100011

correct output
3
1 2 1 

user output
4
1 1 1 1

Test 47

Group: 3

Verdict: ACCEPTED

input
0100111100

correct output
3
1 1 1 

user output
3
1 1 1

Test 48

Group: 3

Verdict: ACCEPTED

input
0001001000

correct output
3
2 2 1 

user output
4
1 1 1 1

Test 49

Group: 3

Verdict:

input
0100100010

correct output
4
2 1 1 1 

user output
QAQ

Test 50

Group: 3

Verdict: ACCEPTED

input
1100101110

correct output
4
2 1 1 1 

user output
4
3 3 2 1

Test 51

Group: 3

Verdict:

input
1000111011

correct output
3
2 1 1 

user output
QAQ

Test 52

Group: 3

Verdict: ACCEPTED

input
1000111111

correct output
2
1 1 

user output
2
1 1

Test 53

Group: 3

Verdict: ACCEPTED

input
0110011100

correct output
3
2 1 1 

user output
4
1 1 1 1

Test 54

Group: 3

Verdict:

input
0001000110

correct output
3
2 2 1 

user output
QAQ

Test 55

Group: 3

Verdict: ACCEPTED

input
1110110010

correct output
4
1 2 1 1 

user output
4
3 2 2 1

Test 56

Group: 3

Verdict: ACCEPTED

input
110011001100110011001100110011...

correct output
QAQ

user output
QAQ

Test 57

Group: 3

Verdict: ACCEPTED

input
110011001100110011001100110011...

correct output
QAQ

user output
QAQ

Test 58

Group: 3

Verdict:

input
110011001100110011001100110011...

correct output
1501
1 1000 1000 1000 1000 1000 100...

user output
QAQ

Test 59

Group: 3

Verdict: ACCEPTED

input
010101010101010101010101010101...

correct output
2498
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
2498
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 60

Group: 3

Verdict:

input
011100011001011111111000010110...

correct output
1272
1 1 648 647 646 646 645 645 64...

user output
QAQ

Test 61

Group: 4

Verdict: ACCEPTED

input
1110101110

correct output
QAQ

user output
QAQ

Test 62

Group: 4

Verdict: ACCEPTED

input
0111001011

correct output
4
2 1 1 1 

user output
4
3 2 1 1

Test 63

Group: 4

Verdict: ACCEPTED

input
1101111101

correct output
3
2 2 1 

user output
3
2 2 1

Test 64

Group: 4

Verdict:

input
1001110001

correct output
3
2 1 1 

user output
QAQ

Test 65

Group: 4

Verdict: ACCEPTED

input
1000000011

correct output
2
1 1 

user output
2
1 1

Test 66

Group: 4

Verdict: ACCEPTED

input
0100010111

correct output
4
1 1 1 1 

user output
4
1 1 1 1

Test 67

Group: 4

Verdict: ACCEPTED

input
0100111010

correct output
4
2 1 1 1 

user output
4
2 1 1 1

Test 68

Group: 4

Verdict: ACCEPTED

input
0010111101

correct output
4
1 1 1 1 

user output
4
1 1 1 1

Test 69

Group: 4

Verdict: ACCEPTED

input
0011111000

correct output
2
2 1 

user output
3
1 1 1

Test 70

Group: 4

Verdict:

input
1101001101

correct output
4
2 2 2 1 

user output
QAQ

Test 71

Group: 4

Verdict: ACCEPTED

input
0110111000

correct output
3
2 1 1 

user output
4
1 1 1 1

Test 72

Group: 4

Verdict: ACCEPTED

input
1100110111

correct output
3
3 2 1 

user output
4
4 3 2 1

Test 73

Group: 4

Verdict: ACCEPTED

input
1110011111

correct output
2
2 1 

user output
3
1 1 1

Test 74

Group: 4

Verdict: ACCEPTED

input
1011000101

correct output
4
2 1 1 1 

user output
4
2 1 1 1

Test 75

Group: 4

Verdict: ACCEPTED

input
1110110010

correct output
4
1 2 1 1 

user output
4
3 2 2 1

Test 76

Group: 4

Verdict: ACCEPTED

input
110011001100110011001100110011...

correct output
QAQ

user output
QAQ

Test 77

Group: 4

Verdict: ACCEPTED

input
110011001100110011001100110011...

correct output
QAQ

user output
QAQ

Test 78

Group: 4

Verdict:

input
110011001100110011001100110011...

correct output
30001
1 20000 20000 20000 20000 2000...

user output
QAQ

Test 79

Group: 4

Verdict:

input
010101010101010101010101010101...

correct output
49998
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 80

Group: 4

Verdict: ACCEPTED

input
111000110000011000001101010010...

correct output
25011
1 12471 12470 12469 12468 1246...

user output
33315
25005 25004 25003 25002 25001 ...