编辑代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// #define MAX_TABLE_LEN 100
#define MAX_STRING 1000
#define MAX_COMM 62

char common[MAX_COMM];

void Harsh(char *q, int *p, int n);
void endHarsh();

int main () {
    int table[150];
    
    int n,m;
    char arr[MAX_STRING];
    scanf("%d",&n);
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        scanf("%s",arr);
        char *q = arr;
        int *p = table;
        for(int i = 0 ; i < 150 ; i++)    table[i] = 0;
        Harsh(q,p,n);
    }

    // for(int i = 0 ; i < strlen(common) ; i++)
    // printf("%c\t",common[i]); 
    for(int i = 0 ; i < 150 ; i++)    table[i] = 0;
    char *a = common;
    endHarsh(a,table,m);

	return 0;
}

void Harsh(char *q, int *p, int n){
    int key;
    while (*q){
        key = *q;
        p[key]++;
        q++;
    }
    char *comm;
    comm = (char*)malloc(sizeof(char)*MAX_COMM);

    int j=0;

    for(int i = 0 ; i < 150 ; i++){
        if(p[i]>=n){
            comm[j] = i; 
            j++;
        }
    }
    // printf("\ncommon before copy:\n");
    // for(int i = 0 ; i < strlen(common) ; i++)
    // printf("%c\t",common[i]);    

    // printf("\ncomm:\n");
    // for(int i = 0 ; i < j ; i++)
    // printf("%c\t",comm[i]);    
    memcpy(common+strlen(common),comm,strlen(comm));
    free(comm);

    // printf("\ncommon after copy:\n");
    // for(int i = 0 ; i < strlen(common) ; i++)
    // printf("%c\t",common[i]);  
    // printf("\n");  

}

void endHarsh(char *a,int *table,int m){
    int key;
    while (*a){
        key = *a;
        table[key]++;
        a++;
    }

    char *comm;
    comm = (char*)malloc(sizeof(char)*MAX_COMM);

    int j=0;

    for(int i = 0 ; i < 150 ; i++){
        if(table[i]>=m){
            comm[j] = i; 
            j++;
        }
    }

    printf("[");
    for(int i = 0 ; i < j ; i++){
        // printf("i is:%d ",i);
        if(i<(j-1)) printf("%c ",comm[i]);
        else printf("%c",comm[i]);
    }
    printf("]");
    // printf("%s",comm);
    free(comm);
    

}