Advertisement
palmerstone

Indexing

Oct 27th, 2011
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cctype>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. struct index
  11. {
  12. char str[90];
  13. int ar2[1000];
  14. int max;
  15. };
  16. struct index ar[5000];
  17.  
  18. int compare(const void *a, const void *b)
  19. {
  20. char str2[100], str3[100];
  21. strcpy(str2, ((index *)a)->str);
  22. strcpy(str3, ((index *)b)->str);
  23.  
  24. if (strcmp(str2, str3) > 0) return 1;
  25. return -1;
  26. }
  27.  
  28. int main()
  29. {
  30. int t = 1, i = 0, j, k, l, a, b, c, d, x, y, z, n, no = 0;
  31. char ch, str2[100], str3[100];
  32. for (i = 0; i < 5000; i++) ar[i].max = 0;
  33.  
  34. //freopen("sample.txt", "r", stdin);
  35. gets(str2);
  36. i = 0, ch = str2[0];
  37. while(gets(str2))
  38. {
  39. l = 0;
  40. for (j = 0; ;j++)
  41. {
  42. if (isalpha(str2[j]))
  43. {
  44. no = 1;
  45. str3[l++] = str2[j];
  46. }
  47. else if (no == 1)
  48. {
  49. no = 0, str3[l] = 0;
  50. if (str3[0] != ch)
  51. {
  52. l = 0;
  53. continue;
  54. }
  55.  
  56. d = 0;
  57. for (k = 0; k < i; k++)
  58. {
  59. if (strcmp(str3, ar[k].str) == 0)
  60. {
  61. d = 1;
  62. if (ar[k].max == 0) ar[k].ar2[ar[k].max++] = t;
  63. else if (ar[k].ar2[ar[k].max - 1] != t) ar[k].ar2[ar[k].max++] = t;
  64. break;
  65. }
  66. }
  67. if (d == 0)
  68. {
  69. strcpy(ar[i].str, str3);
  70. ar[i++].ar2[ar[k].max++] = t;
  71. }
  72. l = 0;
  73. }
  74. if (str2[j] == 0) break;
  75. }
  76. t++;
  77. }
  78. qsort(ar, i, sizeof(index), compare);
  79.  
  80. for (j = 0; j < i; j++)
  81. {
  82. printf("%s", ar[j].str);
  83. for (k = 0; k < ar[j].max; k++) printf(" %d", ar[j].ar2[k]);
  84. putchar(10);
  85. }
  86. return 0;
  87. }
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement