字符串LSD
#include<iostream> #include<string> using namespace std; int n; void LSD(string *a, int N) { int W = a[0].length(), R = 256; string *Aux = new string[N]; int *Count = new int[R + 1]; for (int k = W - 1; k >= 0; k--) { // 初始化索引数组 for (int i = 0; i < R + 1; i++) Count[i] = 0; // 计算位置k上字符频数 for (int i = 0; i < N; i++) Count[a[i][k] + 1]++; // 将频数转换为索引 for (int i = 0; i < R; i++) Count[i + 1] += Count[i]; // 将元素分类 for (int i = 0; i < N; i++) Aux[Count[a[i][k]]++] = a[i]; // 回写 for (int i = 0; i < N; i++) a[i] = Aux[i]; } Aux = NULL; delete Aux; Aux = NULL; delete Count; Count = NULL; for(int i=0;i<N;i++){ cout<<a[i]<<' '; } } int main(){ cout<<"输入要排序的单词总数:"<<endl; cin>>n; string s[n]; cout<<"输入要排序的单词:"<<endl; for(int i=0;i<n;i++){ cin>>s[i]; } LSD(s,n); }