fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. int main()
  5. {
  6. int i,j,c,n,k=0,t;char (*str)[32];int *count;
  7.  
  8. //test cases, must be less than 5
  9.  
  10. scanf("%d",&t);
  11. if (t<=5)
  12. {
  13. while (t--)
  14. {
  15. //number of accounts,must be less than 100000
  16.  
  17. scanf("%d\n",&n);
  18. if (n<=100000)
  19. {
  20. //array str to store input n number of account numbers of same size 32
  21. //array count to store number of times each account number occurs
  22.  
  23. str=(char(*)[32])malloc(n*32*sizeof(char));
  24. count=(int *)malloc(n*sizeof(int));
  25. for (i=0;i<n;i++)
  26. {
  27. for (j=0;(c=getchar())!='\n' && j<31;j++)
  28. str[i][j]=c;
  29.  
  30. str[i][j]='\0';
  31. }
  32. //quick sort function to sort the array in lexicographic order
  33.  
  34. qsort(str, n, 32, (int (*)(const void *, const void *))strcmp);
  35.  
  36. //prints account number along with its frequency
  37. //case when only a single account occurs
  38.  
  39. if (strcmp(str[0],str[n-1])==0)
  40. printf("%s %d\n",str[0],n);
  41.  
  42. //case when multiple accounts are inputted
  43.  
  44. else
  45. {
  46. for (i=0;i<n-1;i=j)
  47. {
  48. for (j=i+1;j<n;j++)
  49. {
  50. if (strcmp(str[i],str[j])!=0)
  51. {
  52. count[k++]=j;
  53. break;
  54. }
  55. else continue;
  56. }
  57. }
  58. for (j=0,i=0;i<k;j=count[i-1])
  59. {
  60. if (i==0)
  61. printf("%s %d\n",str[j],count[i]);
  62. else
  63. printf("%s %d\n",str[j],count[i]-j);
  64. i++;
  65. }
  66. if (strcmp(str[n-1],str[n-2])!=0)
  67. printf("%s %d\n",str[n-1],1);
  68. }
  69. //free memory after every input
  70.  
  71. free(str);
  72. free(count);
  73. }
  74. }
  75. }
  76. return 0;
  77. }
Success #stdin #stdout 0s 9432KB
stdin
1
1
12 12345687 1234 5678 1234 5678
stdout
12 12345687 1234 5678 1234 5678 1