直接定义二维数组,用sort无法排序,但可以吧一维定义在结构体中,在定义结构体数组,就实现了对二维字符数组的排序。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 20000+5;
int a[maxn];
typedef struct {
char dna[30];
}man;
man s[maxn];
bool cmp(man x, man y) {
return strcmp(x.dna, y.dna) < 0;
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m), n||m) {
for (int i = 0; i < n; i ++) {
scanf("%s", s[i].dna);
}
sort(s, s+n, cmp);
for (int i= 0; i <= n; i ++) {
a[i] = 0;
}
int k = 1;
for (int i = 1; i < n; i ++) {
if (strcmp(s[i].dna, s[i-1].dna) == 0) k ++;
else {
a[k] ++;
k = 1;
}
}
a[k] ++;
for (int i = 1; i <= n; i ++) {
printf("%d\n", a[i]);
}
}
return 0;
}