杭电1004 ac code
#include <stdio.h> #include <string.h> #include <stdlib.h> #define STR_LEN 256 struct s_data { char str[STR_LEN]; int counter; }; typedef struct s_data DATA; typedef struct s_data* PDATA; int find(PDATA *p,char * input,int counter) { for (int i = 0 ; i< counter;i++) { if(strcmp(p[i]->str,input) == 0) { return i; } } return -1; } void handle(PDATA * p, char * input,int *counter) { int ret = find(p,input,*counter); if(ret == -1) { int t_len = strlen(input); PDATA tmp_data = (PDATA)malloc(sizeof(DATA)); memset(tmp_data,0,sizeof(DATA)); strncpy(tmp_data->str,input,t_len); tmp_data->counter = 1; p[*counter]=tmp_data; *counter = *counter+1; } else { p[ret]->counter++; } } void print(PDATA *p,int counter) { int max = 0,index=0; for (int i=0;i<counter;i++) { if(p[i]->counter > max) { max=p[i]->counter; index = i; } } printf("%s\n",p[index]->str); } void free(PDATA *p,int counter) { for(int i=0;i<counter;i++) { if(p[i] != NULL) { free(p[i]); p[i] = NULL; } } } int main() { int n = 0; char input[STR_LEN] = {0}; PDATA p_data[1000]={0}; int counter = 0; while(scanf("%d",&n)!=EOF) { counter = 0; memset(p_data,0,sizeof(p_data)); if(n==0) { break; } while(n--) { memset(input,0,sizeof(input)); scanf("%s",input); handle(p_data,input,&counter); } print(p_data,counter); free(p_data,counter); } return 0; }