Блог пользователя Vallerious

Автор Vallerious, история, 7 лет назад, По-английски

Hello!

I would be really thankful if someone can help me with this one.

I am trying to solve: A1281 but on test 4 I get error for "Unexpected EOF". I searched through the platform to find similar posts, but I could not find information on what might be causing this. Here is my code:

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

char contains_last(char* haystack, size_t haystack_size, char* needle, size_t needle_size) {
    if (haystack == NULL || strlen(haystack) == 0 || strlen(needle) == 0 || needle == NULL || haystack_size <= 0 || needle_size <= 0
        || haystack_size < needle_size) {
        return '0';
    }

    // start from the last character and compare the characters one by one
    int haystack_last_char_idx = haystack_size - 1;
    int needle_last_char_idx = needle_size - 1;
    int found_count = 0;

    while (haystack_last_char_idx > -1 && needle_last_char_idx > -1) {
        if (found_count == needle_size) {
            break;
        }

        // printf("Comparing %c and %c \n", haystack[haystack_last_char_idx], needle[needle_last_char_idx]);

        if (haystack[haystack_last_char_idx] != needle[needle_last_char_idx]) {
            return '0';
        }

        found_count++;
        haystack_last_char_idx--;
        needle_last_char_idx--;
    }

    return '1';
}

int main() {
    int test_case_count;
    scanf("%d\n", &test_case_count);
    char* filipino_suffix = "po";
    char* japanese_suffix_1 = "desu";
    char* japanese_suffix_2 = "masu";
    char* korean_suffix = "mnida";
    size_t filip_suff_len = strlen(filipino_suffix);
    size_t jap_suff_1_len = strlen(japanese_suffix_1);
    size_t jap_suff_2_len = strlen(japanese_suffix_2);
    size_t kor_suff_len = strlen(korean_suffix);
    const char* filipino = "FILIPINO";
    const char* japanese = "JAPANESE";
    const char* korean = "KOREAN";

    // For each test case
    for (int i = 0; i < test_case_count; i++) {
        char sentence[1001];
        fgets(sentence, sizeof(sentence), stdin);
        size_t sentence_len = strlen(sentence) - 1;

        if (contains_last(sentence, sentence_len, filipino_suffix, filip_suff_len) == '1') {
            printf("%s\n", filipino);
        } else if (contains_last(sentence, sentence_len, japanese_suffix_1, jap_suff_1_len) == '1' ||
            contains_last(sentence, sentence_len, japanese_suffix_2, jap_suff_2_len) == '1') {
            printf("%s\n", japanese);
        } else if (contains_last(sentence, sentence_len, korean_suffix, kor_suff_len) == '1') {
            printf("%s\n", korean);
        }
    }

    return 0;
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -7
  • Проголосовать: не нравится