为什么提交会出现Runtime Error at Test 1 (ACCESS_VIOLATION)

描述

根据给定的叶结点字符及其对应的权值,进行哈夫曼编码。

输入

第一行为叶子结点的数目n(1<=n<=100)。第二行为一个字符串,包含n个字符,每个字符对应一个叶子结点,第三行为每个叶子结点的概率(即权值),要求根据各叶结点构造哈夫曼值,并进行哈夫曼编码。构造哈夫曼树的原则是先两个最小的,构造一个父结点,其中最小的结点为左孩子,次小的为右孩子,如果两个最小的叶结点相等,则取排在前一个位置的为左孩子。编码原则是左孩子为0,右孩子为1。

输出

根据字符出现的次序,每个字符用一行输出其哈夫曼编码。

样例输入

8
abcdefgh
5 29 7 8 14 23 3 11

代码
#include<iostream>
using namespace std;
typedef struct
{
int value;
int left;
int right;
int father;
}Node;
void f(Node a[],int n)
{
int i,j,t;
int min,min1;
for(i=n+1;i<=2*n-1;i++)
{
min=0;
min1=2*n;
a[min].value=a[min1].value=INT_MAX;
for(j=1;j<i;j++)
{
if(a[j].father==0)
{
if(a[j].value<a[min].value)
{
t=min;
min=j;
min1=t;
}
else if(a[j].value>=a[min].value&&a[j].value<a[min1].value)
{
min1=j;
}
}
}
a[i].value=a[min].value+a[min1].value;
a[i].left=min;
a[i].right=min1;
a[min].father=i;
a[min1].father=i;
}
}
void k(Node b[],int n)
{
int i,u,j,t,m;
char o[1000];
for(i=1;i<=n;i++)
{
u=i;
j=0;
while(b[u].father)
{
m=u;
u=b[u].father;
if(b[u].left==m)
o[j]='0';
else o[j]='1';
j++;
}
for(t=j-1;t>=0;t--)
{
cout<<o[t];
}
cout<<endl;
}
}
int main()
{
int n,i,b;
Node *p;
char ch[1000];
cin>>n;
p=new Node[n*2];
cin>>ch;
for(i=1;i<=n;i++)
{
cin>>b;
p[i].value=b;
p[i].left=p[i].right=p[i].father=0;
}
for(i=n+1;i<=2*n-1;i++)
{
p[i].value=p[i].left=p[i].right=p[i].father=0;
}
f(p,n);
k(p,n);
return 0;
}

第1个回答  2011-05-04
Fur has been 34 months , and have been able to clearly express themselves , but often some very impressive to say the words :

1. Dad liked the little touch plush PP, hairy irritated, Father mother smiled and said, . . . . . Liu called her mother often hairy , evidently meaning she was wrong !

2. for her father likes to touch the issue of small PP , hairy strong views , one day , my father and , in consultation with her ??views, she was very upset and said : Small PP ah , you feel you PP it ! . . . .

4. hairy love songs, songs once heard a bath , she was like learning to sing after the day she heard her mother : ,Discount Oakley sunglasses! One day, her mother taught her the police policeman, this is a bit harder to teach a few times and barely able to speak clearly, two days , my mother asked her how the police that she thought for a long time to answer : . . . . . 5

6. brother hairy While more than two months,Mac eyeshadow, but say relatively late, some words have always said do not know, one day my mother went to kindergarten with a hairy brother, back when passing the park entrance,Wholesale sunglasses, Mao Mao asked: . . .

7.32 months, a mother took her to do a pirate ship and babies screaming scared . After only one mention sit pirate ship, she does not go out killing . . . .

为什么提交会出现Runtime Error at Test 1 (ACCESS_VIOLATION)
6. brother hairy While more than two months,Mac eyeshadow, but say relatively late, some words have always said do not know, one day my mother went to kindergarten with a hairy brother, back when passing the park entrance,Wholesale sunglasses, Mao Mao asked: . . .7.32 months...

...但是提交后提示Runtime Error (ACCESS_VIOLATION)
构造一个矩阵每次都是一个矩阵的转移。然后可以用二分。当然也是有周期的。最大的周期是49 因为这些数字都是要7的范围内 如果有两个数字连续一样的话,后面的数字就会和前面重复 f[i]==f[i+k]&&f[i+1]==f[i+1+k]这样的话后面就会重复的 \/\/此题是一个很典型的递归mod找周期的题目,很...

Runtime Error (ACCESS_VIOLATION) 到底出错在哪里呢?在线等,acm 提交...
while(scanf("%d %d", &n, &m) != EOF) \/\/ctrl + z 表示结束, acm题没说怎么结束用这个。这道题你做的有点麻烦啊,输出的时候这样就可以:num = 0;index = m;whiel(num < n){ if(index != m)printf(" %d", a[index++]);else printf("%d", a[index++]);num++;index =...

测试代码出现Runtime Error(ACCESS_VIOLATION)错误
测试代码出现Runtime Error(ACCESS_VIOLATION)错误 #include<stdio.h>#include<string.h>intmain(){ints,m,n,i,j;charp[10000][11],a[11],q[11];for(i=0;;){gets_s(p[i]);if(p[i][0]=='\\0')break;i+=1;}m=i;for(j=0;;){gets_s(q);s=0;f... #include <stdio.h>#include...

杭电OJ2019题。为什么Runtime Error (ACCESS_VIOLATION)?
if (Num[j]<m) \/\/取消等号,会出现相等的情况 a[j]=Num[j];else { a[j]=m;break;} } while (j<=n-1){ a[j+1]=Num[j]; \/\/ a[j+1]=Num[j];j++;} for (i=0,flag=0;i<=n;i++){ if (flag==1)printf(" ");printf("%d",a[i]);flag=1;} printf("...

...但提交时总是Runtime Error (ACCESS_VIOLATION) 请高手看一下代码...
你定义的数组下标值太大,将1000改小点就可以了,到底具体可以取多大,还不知道,应该不可以超过内存大小吧。希望对你有帮助 可以修改成这样:long **a=(long**)malloc(1000*sizeof(long));long **b=(long**)malloc(1000*sizeof(long));for(i=0;i<1000;i++)a[i]=(long*)malloc(1000*...

Runtime Error(ACCESS_VIOLATION) 总是出现 跪求解答
Runtime Error 就是ACM中常说的RE,出现这种错误往往是数组越界造成的,你应该检查数组开的是否足够大,或者在程序处理过程中是否存在数组下表越界的情况。由于你没有说具体是那道题,因此我也无法提供具体的帮助。

...已提交就是Runtime Error (ACCESS_VIOLATION)啊,请问是哪的问题_百...
runtime - error, 表示运行时错误, (access violation 表示访问越界)问题出在你的p指针上, 当执行完组数据时, 你的p指针没有指向申请的空间的起始地址,而是 当前这组测试数据的最后一个字符的地址, 当执行多组数据后,p指针会指向申请空间意外的地方,所以访问越界。我加了一个q指针, 保存...

OJ上的runtime error一般会在什么情况下出现?
回答:越栈了,比如数组开的不够大 编译错误时Compilation Error Runtime Error (RE) : 运行时错误,这个一般是程序在运行期间执行了非法的操作造成的。以下列出常见的错误类型: ACCESS_VIOLATION 您的程序想从一些非法的地址空间读取或向其中写入内容。一般例如指针、数组下标越界都会造成这个错误的。 ARRAY_...

Runtime Error (ACCESS_VIOLATION) 为什么!!!
一般来讲出现这个问题应该是指针越界了。你查一下看。另外,这句:if(0<n<100000)看上去很诡异的...今天太迟了,先下线了,不帮你试了。好运~

相似回答