CSES - Datatähti 2016 alku - Results
Submission details
Task:Osajono
Sender:RemoveForce
Submission time:2015-10-05 17:30:43
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void readInput()':
input/code.cpp:17:22: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   input = malloc(size);
                      ^
input/code.cpp:21:32: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   char* new_input = malloc(size);
                                ^
input/code.cpp:25:2: error: jump to label 'read' [-fpermissive]
  read:;
  ^
input/code.cpp:18:8: error:   from here [-fpermissive]
   goto read;
        ^
input/code.cpp:21:9: error:   crosses initialization of 'char* new_input'
   char* new_input = malloc(size);
         ^
input/code.cpp: In function 'unsigned int getResults()':
input/code.cpp:45:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

Code

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

char* input;
unsigned counter;

void copy(size_t size, char *src, char *dest)
{
  for (size_t i = 0; i < size; ++i)
    dest[i] = src[i];
}

void readInput()
{
  size_t size = 8, used = 0;
  input = malloc(size);
  goto read;
 reserve:;
  size *= 2;
  char* new_input = malloc(size);
  copy(used, input, new_input);
  free(input);
  input = new_input;
 read:;
  char c = fgetc(stdin);
  if (c == '\n' || c == EOF)
    goto end;
  input[used++] = c;
  if (used == size)
    goto reserve;
  goto read;
 end:;
  input[used] = '\0';
}

unsigned getResults()
{
  size_t size = strlen(input);
  for (size_t i = 0; i < size; ++i)
    for (size_t j = 0; j < size - i; ++j)
      //printf("%c == %c", input[j], input[j + i]);
      if (input[j] == input[j + i])
	++counter;
}

int main()
{
  readInput();
  getResults();
  printf("%d\n", counter);
}