Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include<locale>
  6.  
  7. char GetLower(char c) {
  8. if (c >= 'A' && c <= 'Z')
  9. return c + (int('a') - int('A'));
  10. if (c >= 'А' && c <= 'Я')
  11. return c + 32;
  12. if (c == 'Ё')
  13. return 'ё';
  14. return c;
  15. }
  16.  
  17. bool is_invis(char c) {
  18. return c < 10 && c >= 0;
  19. }
  20.  
  21. bool is_sign(char c) {
  22. return c == ',' || c == '.' || c == ';' || c == ':';
  23. }
  24.  
  25. int calclen(char* word) {
  26. int sz = 0;
  27. while (word && !is_invis(word[0]) && !is_sign(word[0])) {
  28. sz++;
  29. word++;
  30. }
  31. return sz;
  32. }
  33.  
  34. bool KMP(char* w1, char* w2) {
  35. if (calclen(w1) != calclen(w2))
  36. return false;
  37. for (int k = 0; k < calclen(w1); k++)
  38. if (GetLower(w1[k]) != GetLower(w2[k])
  39. && !is_sign(w1[k])
  40. && !is_sign(w2[k])
  41.  
  42. )
  43. return false;
  44. return true;
  45. }
  46.  
  47. void push_back(int* arr, int val, int size) {
  48. for (int i = 0; i < size; i++) {
  49. if (arr[i] == -1) {
  50. arr[i] = val;
  51. break;
  52. }
  53. }
  54. }
  55.  
  56. int main() {
  57.  
  58. FILE *S1, *S2;
  59. S1 = fopen("S1.txt", "r");
  60. S2 = fopen("S2.txt", "w");
  61.  
  62. char words[10000][100];
  63. int words_cnt = 0;
  64. fscanf(S1, "%[^ ]", &words[words_cnt++]);
  65. while (fgetc(S1) != EOF)
  66. fscanf(S1, "%[^ ]", &words[words_cnt++]);
  67. int i = 1;
  68. while (words[i - 1][strlen(words[i - 1]) - 1] != '.')
  69. i++;
  70. int* words_c = (int*)malloc(sizeof(int)*i);
  71. memset(words_c, 0, sizeof(int)*i);
  72. for (int j = i; j < words_cnt; j++) {
  73. for (int l = 0; l < i; l++) {
  74. if (KMP(words[j], words[l]))
  75. words_c[l]++;
  76. }
  77. }
  78. int** strings_ids = (int**)malloc(sizeof(int*)*i);
  79. for (int k = 0; k < i; k++) {
  80. strings_ids[k] = (int*)malloc(sizeof(int)*words_c[k]);
  81. for (int l = 0; l < words_c[k]; l++)
  82. strings_ids[k][l] = -1;
  83. }
  84. int sentence = 2;
  85. for (int j = i; j < words_cnt; j++) {
  86. for (int l = 0; l < i; l++) {
  87. if (KMP(words[j], words[l]))
  88. push_back(strings_ids[l], sentence, words_c[l]);
  89. }
  90. if (words[j][strlen(words[j]) - 1] == '.')
  91. sentence++;
  92. }
  93. for (int j = 0; j < i; j++)
  94. {
  95. int old = 0;
  96. if (j == i - 1) words[j][strlen(words[j])- 1] = ' ';
  97. fprintf(S2, "%s\n", words[j]);
  98. fprintf(S2, "%d\n", words_c[j]);
  99. for (int k = 0; k < words_c[j]; k++) {
  100. if (old == strings_ids[j][k])
  101. continue;
  102. fprintf(S2, "%d ", strings_ids[j][k]);
  103. old = strings_ids[j][k];
  104. }
  105. fprintf(S2, "\n");
  106. }
  107. fclose(S1);
  108. fclose(S2);
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement