Codeforces 4C运行时错误
Codeforces 4C Runtime Error
嗨,我正在使用Trie实现来解决这个问题,但是在codeforces的服务器中,我得到了运行时错误,在我的pc和Ideone上是正常的。有什么想法吗?http://codeforces.com/contest/4/problem/Chttps://ideone.com/dvRL3b
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN ('z'-'a'+2)
#define VALUE(c) ( (c)-'a' )
struct Trie
{
Trie *m[MAXN];
int count;
}trie;
int insert(Trie * t, char * w){
if(w[0]==' '){
t->count++;
return t->count;
}
if(t->m[VALUE(w[0])] == NULL){
t->m[VALUE(w[0])] =(Trie *) malloc(sizeof(Trie));
}
return insert(t->m[VALUE(w[0])] , w+1);
}
int main(){
int t;
scanf("%d",&t);
for (int i = 0; i < t; ++i)
{
char *w = NULL;
w = (char *)malloc(sizeof(char));
scanf("%s",w);
//printf("%sn", w);
int resp = insert(&trie,w);
if(resp > 1){
printf("%s%dn", w,resp-1);
}else{
printf("OKn");
}
}
return 0;
}
这一行:
w = (char *)malloc(sizeof(char));
将只分配一个字节的内存。
这个问题中的请求最多32个字符,所以尝试:
w = (char *)malloc(sizeof(char)*33); // include 1 extra byte for zero termination byte
此外,在为每个新Trie分配内存时,您可能应该使用calloc,即更改
t->m[VALUE(w[0])] =(Trie *) malloc(sizeof(Trie));
t->m[VALUE(w[0])] =(Trie *) calloc(sizeof(Trie),1);
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- C++在使用std::multimap时出现运行时错误的几率很小
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 试图找出为什么我会收到运行时错误?
- 迭代二维矢量时发生运行时错误
- Codeforces 4C运行时错误