-4

このコードは、文字列のlsd基数ソートアルゴリズムを実装するために作成しました。

ロジック:最下位桁から開始し、最初にこの桁に並べ替えを適用します。次に、左の次の桁に移動します。count[]特定の桁位置での各英語アルファベットの頻度が含まれます。のcount[1]頻度を表します'a'...など...および。次に、累積カウントを計算します。ここで、文字列の配列を繰り返し処理し、配列内の適切な位置を計算します。例:特定の数字の位置の場合、その位置にある単語が、、、を占めることを意味します。count[2]'b'count[0]=0auxcount[2] = 4count[1]=1'b'aux[1]aux[2]aux[3]

#include<iostream>
#include<string>
using namespace std;

int main()
{
string arr[]={"dab","cab","fad","bad","dad","ebb","ace","add","fed","bed","fee","bee"};
string aux[12]={"dab","cab","fad","bad","dad","ebb","ace","add","fed","bed","fee","bee"};

int count[27]={0};
for(int i=2;i>=0;i--)
{
for(int j=0;j<12;j++)
count[static_cast<int>(arr[j][i])-64]++;
cout<<"here"<<endl;    //////////////THIS GETS PRINTED

for(int j=0;j<26;j++)
count[j+1]+=count[j]; //calculating cumulative value
cout<<"HERE"<<endl;   ///////////////THIS GETS PRINTED

for(int j=0;j<12;j++)
{
int x=count[static_cast<int>(arr[j][i])-65]; //65 because positions for 'a' will be 
                                             //determined by count[0], for 'b' will be
                                             // determined by count[1] etc.  
aux[x]=arr[j];
cout<<j<<endl;  //even j=0 doesn't get printed
count[static_cast<int>(arr[j][i])-65]++;
}

cout<<"here"<<endl;

for(int j=0;j<12;j++)
cout<<aux[j]<<endl;
} //i governs which digit is being compared

return 0;
}

こことここに印刷した後、コードはsegfaultを与えます。問題は、3番目の'forj'ループにあります。誰かが問題を特定できますか?

4

1 に答える 1

0

小さな間違いがありました...私は「a」ではなく「A」のASCIIである65を引いていました

于 2012-12-30T18:28:49.080 に答える