Как я могу избавиться от этой ошибки во время выполнения? - PullRequest
0 голосов
/ 18 октября 2019

Я хочу решить эту конкретную проблему конкурентного программирования на Codeforces https://codeforces.com/contest/4/problem/C, но я получаю эту ошибку времени выполнения на тесте 7 для n=100000 в качестве ввода и не могу понять, почему. Это ошибка сегментации«Утечка памяти» Несмотря на то, что я могу взглянуть на конкретные тесты, по которым оценивается мой код, все, что может сказать электронный судья, это Exit code is -1073740940.

Вот мой код:

#include<iostream>
#include<cctype>
#include<cstring>
#define HASH_KEY 'a'
#define MAX_RANGE 4093
using namespace std;
typedef struct Node {
    char* user_name;
    struct Node* next;
};
int string_to_int(char *current_string);
void Add_Element(Node** head, char *current_string);
bool is_already_user_name(Node** head, char* current_string);
int string_to_int(char *current_string) {
    int current_sum = 0;
    for (int i = 0; current_string[i] != '\0'; i++)
        current_sum += int(current_string[i]);
    return current_sum;
}
void Add_Element(Node** head,char *current_string) {
    int key = string_to_int(current_string) - HASH_KEY;
    if (head[key]==NULL) {
        head[key] = new Node;
        head[key]->user_name = new char[strlen(current_string) + 1];
        strcpy(head[key]->user_name, current_string);
        head[key]->next = NULL;
    }
    else {
        Node *temp = new Node;
        temp->user_name = new char[strlen(current_string) + 1];
        strcpy(temp->user_name, current_string);
        temp->next=head[key];
        head[key] = temp;
    }
}
bool is_already_user_name(Node** head,char* current_string) {
    int key = string_to_int(current_string) - HASH_KEY;
    if (head[key]==NULL)
        return false;
    for (Node* traversal_pointer = head[key]; traversal_pointer; traversal_pointer = traversal_pointer->next)
        if (strcmp(head[key]->user_name, current_string) == 0)
            return true;
    return false;
}
int main()
{
    int n;
    char current_string[33];
    char buffer[6];
    Node** hashtable=new Node*[MAX_RANGE]();
    cin >> n;
    int no_current_user=0;
    for (;n;n--) {
        scanf("%s", current_string);
        if (is_already_user_name(hashtable, current_string)) {
            while (is_already_user_name(hashtable, current_string)) {
                if (isdigit(current_string[strlen(current_string) - 1]))
                    current_string[strlen(current_string)-1] = '\0';
                strcat(current_string, _itoa(++no_current_user, buffer, 10));
            }
            no_current_user = 0;
            Add_Element(hashtable, current_string);
            printf("%s\n", current_string);
        }
        else {
            Add_Element(hashtable, current_string);
            printf("OK\n");
        }
    }
    delete[]hashtable;
    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...