我想在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;
}