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

Compiler report

input/code.cpp: In function 'void readInput()':
input/code.cpp:18:22: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   input = malloc(size);
                      ^
input/code.cpp:22:32: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   char* new_input = malloc(size);
                                ^
input/code.cpp:26:2: error: jump to label 'read' [-fpermissive]
  read:;
  ^
input/code.cpp:19:8: error:   from here [-fpermissive]
   goto read;
        ^
input/code.cpp:22:9: error:   crosses initialization of 'char* new_input'
   char* new_input = malloc(size);
         ^

Code

extern "C" {
#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';
}

void 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);
}
}