Submission details
Task:Bittipeli
Sender:hello_world
Submission time:2015-10-02 23:52:31 +0300
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1details
#20.05 s1details
#30.05 s1details
#40.05 s1details
#50.06 s1details
#60.06 s1details
#70.06 s1details
#80.05 s1details
#90.06 s1details
#100.05 s1details
#110.05 s1details
#120.06 s1details
#130.05 s1details
#140.05 s1details
#150.05 s1details
#160.06 s1details
#17ACCEPTED0.05 s1details
#180.05 s1details
#190.06 s1details
#20ACCEPTED0.06 s1details
#21ACCEPTED0.05 s2details
#220.06 s2details
#230.06 s2details
#240.05 s2details
#250.06 s2details
#260.04 s2details
#270.05 s2details
#280.05 s2details
#290.05 s2details
#300.06 s2details
#310.05 s2details
#320.06 s2details
#330.05 s2details
#340.05 s2details
#350.06 s2details
#360.05 s2details
#370.06 s2details
#380.06 s2details
#390.06 s2details
#400.06 s2details
#41ACCEPTED0.05 s3details
#420.05 s3details
#430.06 s3details
#440.04 s3details
#450.05 s3details
#460.05 s3details
#470.06 s3details
#480.05 s3details
#490.06 s3details
#500.05 s3details
#510.06 s3details
#520.05 s3details
#530.06 s3details
#540.05 s3details
#550.06 s3details
#560.06 s3details
#570.05 s3details
#580.06 s3details
#590.05 s3details
#600.06 s3details
#61ACCEPTED0.06 s4details
#620.06 s4details
#630.05 s4details
#640.06 s4details
#650.05 s4details
#660.05 s4details
#670.06 s4details
#680.05 s4details
#690.05 s4details
#700.06 s4details
#710.06 s4details
#720.06 s4details
#730.06 s4details
#740.06 s4details
#750.05 s4details
#760.07 s4details
#770.06 s4details
#780.05 s4details
#790.05 s4details
#800.06 s4details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:84:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < length; i++){
                     ^
input/code.cpp:87:45: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (cur_bit != ap->bit || (lastBits && i == length - 1)) {
                                             ^
input/code.cpp:90:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      if (i == length - 1) {
            ^
input/code.cpp:43:7: warning: unused variable 'bit' [-Wunused-variable]
  char bit;
       ^
input/code.cpp:46:7: warning: unused variable 'last_bit' [-Wunused-variable]
  bool last_bit;
       ^
input/code.cpp:74:28: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   fgets(buffer, len, stdin);
                            ^

Code

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>

using namespace std;

struct Bits{
	
	Bits *next;
	Bits *last;
	
	bool onlyOneBit;//true = vain yksi bitti, false = yli yksi bitti
	bool bit;	// bit == true == 1, bit == false == 0
	
	unsigned int index;
};


struct Index{
	unsigned int value;
	Index *next;
};

Bits *temp, *temp2, *temp3;
bool lastBits = false;
bool lastBit = false;

void printBits(Bits *a);
bool deleteBits(Bits *a);
void addIndex(unsigned int index);
void printIndex();
void freeIndex();
void freeMemory(Bits *a);

Index *index_a = (Index *)malloc(sizeof(Index *));
Index *index_ap = index_a;	//index ap on listan loppu ja index_a on listan alku
//bool init_index = true;

int main(void){
	
	char bit;
	
	bool cur_bit;
	bool last_bit;
	bool onlyOneBit;
	
	index_ap->next = NULL;
	
	Bits *a = (Bits *)malloc(sizeof(Bits *));
	a->bit = (fgetc(stdin) == '1');	//otetaan ensimmäinen bit, jotta sitä ei pidä joka kerta tarkistaa while-lauseen for-lauseessa, tms, jne..
	//(buffer[i] == '1')
	if (feof(stdin)) {
		cout << endl << "QAQ";	//vain yksi bit syötetty = yhtä ei voida poistaa = häviö
		return 0;
	}
	a->next = NULL;
	a->last = NULL;
	a->index = 0;
	
	
	Bits *ap = a;
	onlyOneBit = true;
	
	unsigned int len = 256;
	unsigned int length;
	char buffer[len];
	
	while(!lastBit) {
		
		//cout << "asdf";
		
		fgets(buffer, len, stdin);
		length = strlen(buffer);
		if (buffer[length - 1] == '\n') {
			buffer[length - 1] = buffer[length - 2];
			//length--;	//'poistetaan' '\n'
			//length--;
			lastBits = true;
			//nyt voidaan poistaa myös ne Bits:it, joiden ap->last ja ap->next on NULL
		}
		//muodostetaan ihan ensimmäiseksi lista, sitten tehdään niistä pointersToBits
		for (int i = 0; i < length; i++){
			if (buffer[i] == '1') cur_bit = 1; 
			else cur_bit = 0;
			if (cur_bit != ap->bit || (lastBits && i == length - 1)) {
				
				if (lastBits) {
					if (i == length - 1) {
						lastBit = true;
						//printBits(a);
						//cout << "ekomatti1 << " << buffer << " " << i << " ";
						
					}
				}
				
				//eri bitti, vaihdetaan
				if (ap->last != NULL) ap->index = (ap->last)->index + 1;
				ap->onlyOneBit = onlyOneBit;
				
				
				//ny on uusi alkio linkattu
				//ap on nyt saatu uusi alkio
				//tarkistetaan voidaanko poistaa jokin alkio
				
				
				if (deleteBits(ap)) {	//ap, tai temp voitiin poistaa
					if (lastBit) {
						//cout << "ap:" << (ap->last)->index << " " << (ap->last)->bit << " " << (ap->last)->onlyOneBit << endl;
						/*while (*/deleteBits(ap);//);
					}
					//cout << endl;printBits(a);
				} else if (lastBit) {	//alkioita ei voitu poistaa, viimeiset alkiot ovat menossa -> peli on päättynyt, peliä ei voitu ratkaista
					//cout << endl;printBits(a);
					cout << endl << "QAQ";
					//printIndex();
					freeMemory(a);
					freeIndex();
					return 0;
				}
				
				ap->next = (Bits *)malloc(sizeof(Bits *));
				(ap->next)->last = ap;
				ap = ap->next;
				ap->bit = cur_bit;
				//ap->onlyOneBit = false;
				ap->next = NULL;
				
				onlyOneBit = true;
				
				
				
			} else if (onlyOneBit){
				onlyOneBit = false;
			}
		}
		
	} 
	
	//printBits(a);	//tulostaa bitit a:sta alkaen
	
	printIndex();	//tulostaa indeksit
	freeIndex();
	freeMemory(a);
	
	return 0;
}

bool deleteBits(Bits *ap){			//ottaa syötteenä ap:n, eikä temp:iä poistettavasta alkiosta, sillä silloin saatettaisiin joutua tekemään turhaa kaksinkertaista tarkistusta, ja jouduttaisiinki
	
	if ((temp = ap->last) != NULL) {
		if ((temp = temp->last) != NULL){	//taaksepäin on linkki = temp, eli poistettava alkio on olemassa <-> eteenpäin on linkki, sillä ap on olemassa
			//if (lastBit) cout << "asdfasdf"; pitäisi pitää paikkansa
			//cout << "asf";
			//tarkastetaan ensin tilanne, jossa molemmat linkit ovat täysiä
			//kaikissa näissä temp:n pitää !onlyOneBit, sillä yhden bitin jonoja ei voida poistaa
			
			if (!temp->onlyOneBit){	//vasemmalle linkattava on yli yhden bitin, tarkistetaan onko sillä linkki toiseenkin suuntaan
				
				if ((temp->last) != NULL) {
					if ((temp->last)->last != NULL){	//linkki löytyi, temp-alkio voidaan poistaa
						//yhdistetään(, ei linkata) temp:iä seuraava ja sitä edeltävä alkio toisiinsa
						//temp2 on temp:n vasen, eli last-puoli ja temp3 on oikea, eli next-puoli
						temp2 = temp->last;						//osoitetaan temp:lla temp:n lastiin, jotta voidaan poistaa temp
						temp2->onlyOneBit = false;				//uusi koko on aina väh.2bit
						//temp2->bit							// ei muutu
						temp3 = temp->next;
						//ap:n indeksiksi tulee poisttavan, eli temp:n indeksi
						ap->index = temp->index;				//muita indeksejä ei pidä vaihtaa, temp2:n indeksi säilyy temp2:sen ja temp3:sen hybridillä
						addIndex(temp->index);					//lisätään index myös listaan
						free(temp);								//voidaan poistaa temp, sillä molempiin sen ympärillä oleviin alkioihin osoitetaan jo
						//säilytetään ainoastaan temp2, sillä siinä on kaikki tarvittava info -> aina kun poistetaan yksi alkio, poistetaan käytännössä kaksi, paitsi kun poistetaaan viimeinen alkio, jos poistetaan
						temp2->next = temp3->next;	//nyt kaikki info on temp2:ssa, joten:
						ap->last = temp2;
						
						free(temp3);
						
						return true;		//alkio voitiin poistaa
					}
				} else {
					if (lastBit) {
						goto A;
					}
				}
			} else goto A;
			//return false;	//turhaan, sillä lopussa on kuitenkin return false
		} else goto A;	//lastbit, 1 takana
	} else {	//ap:lla ei ole vasemmalle linkkiä, joten poistettavalla alkiolla(== ap) ei ole myöskään varmasti oikealle päin linkkiä
		
		A:		//jos linkkejä ei ole kumpaankaan suuntaan, ainoastaan ->last->last:n puuttuessa on tämä tilanne, sillä ->last->last:lla on aina ->next->next
		//->tarkistetaan onko peli päättynyt = ei tule lisää merkkejä = lastBits == true
		//cout << "ekomatti << ";
		
		if (lastBit){	//jos peli on päättynyt
			//cout << "ekomatti2 << ";
			if (ap->last != NULL) {
				ap = ap->last;
			} 
			if (!ap->onlyOneBit) {	//jos ap, poistettava alkio on yli yhden bitin bittainen, peli voidaan voittaa = alkio voidaan poistaa
				
				temp = ap;					//pointataan ap:n pointtaamaan alkioon, sillä ap pitää poistaa, mutta ap:n pitää toisaalta säilyä
				
				addIndex(ap->index);
				if ((ap = ap->last) != NULL) {	//jos alkioita = alkio on vielä jäljellä
					//(yläpuoli)siirretään ap yhden alkion takaisinpäin, sillä edellinen alkio = uusin alkio = listan pää poistetaan
					if ((temp2 = temp->next/*==ap->next->next*/) != NULL){	//myös ap:n seuraava alkio on olemassa != mahdotonta, mutta harvinaista
						//yhdistetään alkiot ap ja ap->next->next
						//ap->onlyOneBit = false;		//aivan turha laittaa falseksi, sillä tiedetään että se on aina false ja se poistetaan nyt jokatapauksessa
						addIndex(ap->index);			//lisätään ap:n indeksi, sillä se poistetaan nytten
						//indeksi jää samaksi, mikä se nyt on
						
						if (ap->last == NULL && temp2->next == NULL) {
							//poistetaan temp, ja yhdistetään ap ja temp2
							
							return true;
						}
						
						//free(temp->next);	//poistetaan vanha alkio
						//free(ap);			//ja myös sen toinen osa, eli uusi hybridi, sillä peli on jokatapauksessa nyt päättynyt, joko häviöön tai voittoon
						//cout << "last";
						//kaikki ap, temp, temp->next on poistettu
						//cout << "asdf";
					}
				} else return true;
				//free(temp);	//ap voi olla joko null tai ap->last, eli toivottavasti eka alkio, jos halutaan voittaa
			} else {	//muutoin peliä ei voida voittaa = alkiota ei voida poistaa = peli on päättynyt
				return false;
			}
		}
		
	}	/*otherwise reutnr false*/
	
	//tarkastetaan sitten tilanne, jossa molemmat linkit ovat tyhiä
	
	
	//ja näiden else, eli tilanne, jossa toinen linkeistä on tyhjä saadaan 'else'.llä
	//täällä poistetaan ne alkiot, 
	
	
	return false;
}

void freeMemory(Bits *a){
	if (a==NULL) return;
	Bits *ap = a;
	Bits *ap2;
	do{
		ap2 = ap->next;
		free(ap);
		ap = ap2;
	}while(ap2 != NULL) ;
}

void freeIndex(){
	if (index_a==NULL) return;
	Index *ap = index_a;
	Index *ap2;
	do{
		ap2 = ap->next;
		free(ap);
		ap = ap2;
	}while(ap2 != NULL) ;
}


void addIndex(unsigned int index){
	static bool init_index = true;
	if (!init_index) {
		index_ap->next = (Index *)malloc(sizeof(Index *));		//luodaan uusi alkio
		index_ap = index_ap->next;								//siirrytään siihen
	} else init_index = false;
	index_ap->value = index;								//muokataan sen arvo ja 
	index_ap->next = NULL;									//alustetaan seuraava ap NULL == 0:ksi
}

void printBits(Bits *a){
	if (a==NULL) return;
	Bits *ap = a;
	do{
		cout << "bit: " << ap->onlyOneBit << " " << endl;
	}while((ap = ap->next) != NULL) ;
}

void printIndex(){
	if (index_a == NULL) return;
	Index *ap = index_a;
	do{
		cout << ap->value << " ";
	}while((ap = ap->next) != NULL) ;
}








Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
1011001010

correct output
QAQ

user output

QAQ

Test 2

Group: 1

Verdict:

input
0000001001

correct output
3
1 1 1 

user output

QAQ

Test 3

Group: 1

Verdict:

input
0111011100

correct output
3
2 1 1 

user output

QAQ

Test 4

Group: 1

Verdict:

input
0100100100

correct output
4
2 1 1 1 

user output
2 2 1 0 

Test 5

Group: 1

Verdict:

input
1110010110

correct output
4
2 1 1 1 

user output

QAQ

Test 6

Group: 1

Verdict:

input
1111110110

correct output
3
1 1 1 

user output

QAQ

Test 7

Group: 1

Verdict:

input
0011110001

correct output
3
1 2 1 

user output

QAQ

Test 8

Group: 1

Verdict:

input
0111111000

correct output
2
1 1 

user output
1 0 1 0 

Test 9

Group: 1

Verdict:

input
1111111100

correct output
2
1 1 

user output
0 0 

Test 10

Group: 1

Verdict:

input
1000010011

correct output
3
2 1 1 

user output

QAQ

Test 11

Group: 1

Verdict:

input
1101110000

correct output
3
1 1 1 

user output

QAQ

Test 12

Group: 1

Verdict:

input
1101101100

correct output
4
1 2 1 1 

user output

QAQ

Test 13

Group: 1

Verdict:

input
0100111110

correct output
3
1 1 1 

user output
2 1 0 

Test 14

Group: 1

Verdict:

input
1101001011

correct output
4
2 2 2 1 

user output
3 2 1 0 

Test 15

Group: 1

Verdict:

input
1110110010

correct output
4
1 2 1 1 

user output

QAQ

Test 16

Group: 1

Verdict:

input
0011011100

correct output
3
3 2 1 

user output

QAQ

Test 17

Group: 1

Verdict: ACCEPTED

input
1100101011

correct output
QAQ

user output

QAQ

Test 18

Group: 1

Verdict:

input
1101100111

correct output
3
2 2 1 

user output
2 1 0 

Test 19

Group: 1

Verdict:

input
0110000100

correct output
3
2 1 1 

user output
2 1 0 

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:

input
1110010000

correct output
3
2 1 1 

user output

QAQ

Test 23

Group: 2

Verdict:

input
1001101100

correct output
4
1 1 1 1 

user output

QAQ

Test 24

Group: 2

Verdict:

input
0000000111

correct output
2
1 1 

user output
0 0 

Test 25

Group: 2

Verdict:

input
0011111110

correct output
2
2 1 

user output
1 0 1 0 

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:

input
0000011011

correct output
3
2 1 1 

user output

QAQ

Test 29

Group: 2

Verdict:

input
1110011101

correct output
3
3 2 1 

user output
2 1 0 

Test 30

Group: 2

Verdict:

input
0001011011

correct output
4
1 1 1 1 

user output
3 2 1 

Test 31

Group: 2

Verdict:

input
0010110011

correct output
4
1 1 1 1 

user output
3 2 1 

Test 32

Group: 2

Verdict:

input
0011100000

correct output
2
2 1 

user output
1 0 1 0 

Test 33

Group: 2

Verdict:

input
1110100110

correct output
4
1 1 1 1 

user output
3 2 1 

Test 34

Group: 2

Verdict:

input
0110110111

correct output
4
2 1 1 1 

user output

Test 35

Group: 2

Verdict:

input
1110110010

correct output
4
1 2 1 1 

user output

QAQ

Test 36

Group: 2

Verdict:

input
110011001100110011001010101010...

correct output
QAQ

user output
2 2 2 2 62 61 60 59 58 57 56 5...

Test 37

Group: 2

Verdict:

input
110011001100110011001100101010...

correct output
QAQ

user output
2 2 2 2 2 52 51 50 49 48 47 46...

Test 38

Group: 2

Verdict:

input
110011001100110011001100110011...

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

user output
2 2 2 2 2 2 2 2 2 22 21 20 19 ...

Test 39

Group: 2

Verdict:

input
010101010101010101010101010101...

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

user output

QAQ

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:

input
1110000001

correct output
2
2 1 

user output
1 0 1 0 

Test 45

Group: 3

Verdict:

input
0001001110

correct output
3
2 2 1 

user output
2 1 0 

Test 46

Group: 3

Verdict:

input
0011100011

correct output
3
1 2 1 

user output

QAQ

Test 47

Group: 3

Verdict:

input
0100111100

correct output
3
1 1 1 

user output
2 1 0 

Test 48

Group: 3

Verdict:

input
0001001000

correct output
3
2 2 1 

user output
2 1 0 

Test 49

Group: 3

Verdict:

input
0100100010

correct output
4
2 1 1 1 

user output
2 2 1 0 

Test 50

Group: 3

Verdict:

input
1100101110

correct output
4
2 1 1 1 

user output

QAQ

Test 51

Group: 3

Verdict:

input
1000111011

correct output
3
2 1 1 

user output
2 1 0 

Test 52

Group: 3

Verdict:

input
1000111111

correct output
2
1 1 

user output
1 0 1 0 

Test 53

Group: 3

Verdict:

input
0110011100

correct output
3
2 1 1 

user output
2 1 0 

Test 54

Group: 3

Verdict:

input
0001000110

correct output
3
2 2 1 

user output
2 1 0 

Test 55

Group: 3

Verdict:

input
1110110010

correct output
4
1 2 1 1 

user output

QAQ

Test 56

Group: 3

Verdict:

input
110011001100110011001100110011...

correct output
QAQ

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

Test 57

Group: 3

Verdict:

input
110011001100110011001100110011...

correct output
QAQ

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

Test 58

Group: 3

Verdict:

input
110011001100110011001100110011...

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

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

Test 59

Group: 3

Verdict:

input
010101010101010101010101010101...

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

user output

QAQ

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:

input
0111001011

correct output
4
2 1 1 1 

user output

QAQ

Test 63

Group: 4

Verdict:

input
1101111101

correct output
3
2 2 1 

user output
2 1 0 

Test 64

Group: 4

Verdict:

input
1001110001

correct output
3
2 1 1 

user output
2 1 0 

Test 65

Group: 4

Verdict:

input
1000000011

correct output
2
1 1 

user output
1 0 1 0 

Test 66

Group: 4

Verdict:

input
0100010111

correct output
4
1 1 1 1 

user output

QAQ

Test 67

Group: 4

Verdict:

input
0100111010

correct output
4
2 1 1 1 

user output

QAQ

Test 68

Group: 4

Verdict:

input
0010111101

correct output
4
1 1 1 1 

user output
3 2 1 

Test 69

Group: 4

Verdict:

input
0011111000

correct output
2
2 1 

user output
1 0 1 0 

Test 70

Group: 4

Verdict:

input
1101001101

correct output
4
2 2 2 1 

user output
3 2 1 0 

Test 71

Group: 4

Verdict:

input
0110111000

correct output
3
2 1 1 

user output

QAQ

Test 72

Group: 4

Verdict:

input
1100110111

correct output
3
3 2 1 

user output
2 1 0 

Test 73

Group: 4

Verdict:

input
1110011111

correct output
2
2 1 

user output
1 0 1 0 

Test 74

Group: 4

Verdict:

input
1011000101

correct output
4
2 1 1 1 

user output

QAQ

Test 75

Group: 4

Verdict:

input
1110110010

correct output
4
1 2 1 1 

user output

QAQ

Test 76

Group: 4

Verdict:

input
110011001100110011001100110011...

correct output
QAQ

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

Test 77

Group: 4

Verdict:

input
110011001100110011001100110011...

correct output
QAQ

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

Test 78

Group: 4

Verdict:

input
110011001100110011001100110011...

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

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

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

QAQ

Test 80

Group: 4

Verdict:

input
111000110000011000001101010010...

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

user output

QAQ