Advertisement
berinkaq

Untitled

Jun 4th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main() {
  6. FILE *in, *out;
  7.  
  8. if(!(in=fopen("data.txt","r"))){
  9. printf("can't open data.txt\n");
  10. return -1;
  11. }
  12. if(!(out=fopen("result.txt","w"))){
  13. printf("can't open result.txt\n");
  14. fclose(in);
  15. return -1;
  16. }
  17.  
  18. int m, n;
  19. scanf("%d %d", &n, &m);
  20.  
  21. char s[80];
  22. scanf("%s", s);
  23.  
  24. int *column = calloc(m, sizeof(int));
  25. int *index = calloc(m, sizeof(int));
  26. int *above = calloc(m, sizeof(int));
  27.  
  28. int ans = 0;
  29. char curr;
  30.  
  31. for (int i = 0; i < n; ++i) {
  32. //для проверки по горизонтали
  33. char prev = ' ';
  34. int len_curr = 0;
  35.  
  36. for (int j = 0; j < m; ++j) {
  37. curr = fgetc(in);
  38. /*
  39. if (curr == ' ') {
  40. above[i] = 0;
  41. prev = ' ';
  42. continue;
  43. }
  44. */
  45. if (column[j]) {
  46. //printf("%d ", j);
  47. //если мы уже начали идти по столбцу
  48. if (index[j] == strlen(s)) {
  49. ++ans;
  50. column[j] = 0;
  51. index[j] = 0;
  52. }
  53. if (curr == s[index[j]])
  54. ++index[j];
  55. else {
  56. column[j] = 0;
  57. }
  58. } else {
  59. //если не начинали идти по столбцу
  60. if (!above[j]) {
  61. if (curr == s[0]) {
  62. column[j] = index[j] = 1;
  63. }
  64. }
  65. }
  66.  
  67. if (!len_curr) {
  68. if (len_curr == strlen(s)) {
  69. ++ans;
  70. len_curr = 0;
  71. }
  72. if (curr == s[len_curr])
  73. ++len_curr;
  74. else
  75. len_curr = 0;
  76. }
  77.  
  78. prev = curr;
  79. above[j] = curr == ' ' ? 0 : 1;
  80. }
  81. }
  82. printf("%d\n", ans);
  83. free(column);
  84. free(index);
  85. free(above);
  86. fclose(in);
  87. fclose(out);
  88. return 0;
  89. }
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement