הערה (קובץ קוד)
קובץ זה הוא קובץ קוד מסוג C.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define DONT_KNOW "I_dont_know"
void printIDontKnow()
{
printf("%s", DONT_KNOW);
exit(0);
}
int examA_q2(char **arr, int n, int m);
//Do not change these functions
void free_string_arr(char **arr, int n)
{
for (int i = 0; i < n; ++i)
{
free(arr[i]);
}
free(arr);
}
bool read_string(char **s)
{
int len;
if (scanf("%d", &len) != 1)
{
return false;
}
*s = malloc((len + 1) * sizeof(char));
if (!*s)
{
return false;
}
if (scanf("%s", *s) != 1)
{
free(*s);
return false;
}
return true;
}
int main(void)
{
// uncomment next line if you don't know the answer
// printIDontKnow();
int n;
if (scanf("%d", &n) != 1)
return 1;
char **arr = malloc(sizeof(char *) * n);
if (!arr)
return 1;
for (int i = 0; i < n; i++)
{
if (!read_string(arr + i))
{
free_string_arr(arr, i);
return 1;
}
}
int m;
if (scanf("%d", &m) != 1)
{
free_string_arr(arr, n);
return 1;
}
printf("%d", examA_q2(arr, n, m));
free_string_arr(arr, n);
return 0;
}
int getLineLength(char **arr, int startWord, int endWord)
{
int len = 0;
for (int i = startWord; i <= endWord; i++)
{
len += strlen(arr[i]);
}
return len;
}
int getWordMin(char **arr, int n)
{
int min = strlen(arr[0]);
for (int i = 1; i < n; i++)
{
int len = strlen(arr[i]);
if (len < min)
{
min = len;
}
}
return min;
}
#define IS_DIGIT(c) (c >= '0' && c <= '9')
int examA_q2(char **arr, int n, int m)
{
// if there are more lines then words, each word can be on a separate line.
if (m >= n)
{
return getWordMin(arr, n);
}
int absoluteMinimum = -1;
// I coun't think fast and finish this in time, so:
// Take the full line and start removing words from the end.
// if its posible each word on a new line.
// if there are no more lines, we need to check the lest lines and figure
// out if moving the next word down will cost more then moving the last word
// up and putting the new word in a separte line.
// each time we do this we will save the absoluteMinimum and thats what we
// will get at the end.
// return
return absoluteMinimum;
}