fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7.  
  8. typedef struct{
  9. char **words;
  10. size_t currentSize;
  11. size_t capacity;
  12. }Words;
  13.  
  14.  
  15. typedef struct {
  16. bool up,down,right,left,upRight,upLeft,downRight,downLeft;
  17. }Direction;
  18.  
  19. void startALloc (Words *words){
  20. words->words = (char**) malloc(10 * sizeof(char*));
  21. words->capacity = 10;
  22. words->currentSize = 0;
  23. }
  24.  
  25. void resize (Words *words){
  26. if (words->currentSize == words->capacity){
  27. words->capacity = words->capacity * 2 + 4;
  28. words->words = (char**) realloc(words->words,words->capacity * sizeof (char*));
  29. }
  30. }
  31.  
  32. void removeWhitespace(char *str) {
  33. size_t i = 1, j = 0;
  34.  
  35. while (str[i] != '\0' ) {
  36. if (!isspace(str[i])) {
  37. str[j++] = str[i];
  38. }
  39. i++;
  40. }
  41. str[j] = '\0';
  42. }
  43.  
  44.  
  45. void makeString (Words words, size_t lenOfWords,int** usableWords){
  46. size_t count = 1;
  47. printf("Tajenka:\n");
  48.  
  49. for (size_t i = 0; i < words.currentSize; ++i) {
  50.  
  51. for (size_t j = 0; j < lenOfWords-1; ++j) {
  52.  
  53. if (words.words[i][j] != '.' && usableWords[i][j] != 1){
  54. printf("%c",words.words[i][j]);
  55. if (count == 60){
  56. printf("\n");
  57. count = 0;
  58. }
  59. count++;
  60. }
  61. }
  62. }
  63. if (count != 1)
  64. printf("\n");
  65. }
  66. void removeUp (size_t len, size_t row , size_t col, int** usableWords) {
  67. for (size_t i = 0; i < len; i++) {
  68. size_t r = row - i;
  69.  
  70. usableWords[r][col] = 1;
  71. }
  72. }
  73. void removeDown (size_t len, size_t row , size_t col, int** usableWords) {
  74. for (size_t i = 0; i < len; i++) {
  75. size_t r = row + i;
  76.  
  77. usableWords[r][col] = 1;
  78. }
  79. }
  80. void removeLeft (size_t len, size_t row , size_t col, int** usableWords) {
  81. for (size_t i = 0; i < len; i++) {
  82. size_t c = col - i;
  83.  
  84. usableWords[row][c] = 1;
  85. }
  86. }
  87. void removeRight (size_t len, size_t row , size_t col, int** usableWords) {
  88. for (size_t i = 0; i < len; i++) {
  89. size_t c = col + i;
  90.  
  91. usableWords[row][c] = 1;
  92. }
  93. }
  94. void removeUpLeft (size_t len, size_t row , size_t col, int** usableWords) {
  95. for (size_t i = 0; i < len; i++) {
  96. size_t r = row - i;
  97. size_t c = col - i;
  98.  
  99. usableWords[r][c] = 1;
  100. }
  101. }
  102. void removeUpRight (size_t len, size_t row , size_t col, int** usableWords) {
  103. for (size_t i = 0; i < len; i++) {
  104. size_t r = row - i;
  105. size_t c = col + i;
  106.  
  107. usableWords[r][c] = 1;
  108. }
  109. }
  110. void removeDownLeft (size_t len, size_t row , size_t col, int** usableWords) {
  111. for (size_t i = 0; i < len; i++) {
  112. size_t r = row + i;
  113. size_t c = col - i;
  114.  
  115. usableWords[r][c] = 1;
  116. }
  117. }
  118. void removeDownRight (size_t len, size_t row , size_t col, int** usableWords) {
  119. for (size_t i = 0; i < len; i++) {
  120. size_t r = row + i;
  121. size_t c = col + i;
  122.  
  123. usableWords[r][c] = 1;
  124. }
  125. }
  126.  
  127. bool checkUp (Words words , char *subStr , size_t row , size_t col){
  128. size_t len = strlen(subStr);
  129. for (size_t i = 0; i < len; i++) {
  130. size_t r = row - i;
  131.  
  132. if (words.words[r][col] != subStr[i]) {
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138.  
  139. bool checkDown (Words words , char *subStr , size_t row , size_t col){
  140. size_t len = strlen(subStr);
  141. for (size_t i = 0; i < len; i++) {
  142. size_t r = row + i;
  143.  
  144. if (words.words[r][col] != subStr[i]) {
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. bool checkRight (Words words , char *subStr , size_t row , size_t col){
  151. size_t len = strlen(subStr);
  152. for (size_t i = 0; i < len; i++) {
  153. size_t c = col + i;
  154.  
  155. if (words.words[row][c] != subStr[i]) {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. bool checkLeft (Words words , char *subStr , size_t row , size_t col){
  162. size_t len = strlen(subStr);
  163. for (size_t i = 0; i < len; i++) {
  164. size_t c = col - i;
  165.  
  166. if (words.words[row][c] != subStr[i]) {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. bool checkUpRight (Words words , char *subStr , size_t row , size_t col){
  173. size_t len = strlen(subStr);
  174. for (size_t i = 0; i < len; i++) {
  175. size_t r = row - i;
  176. size_t c = col + i;
  177.  
  178. if (words.words[r][c] != subStr[i]) {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. bool checkUpLeft (Words words , char *subStr , size_t row , size_t col){
  185. size_t len = strlen(subStr);
  186. for (size_t i = 0; i < len; i++) {
  187. size_t r = row - i;
  188. size_t c = col - i;
  189.  
  190. if (words.words[r][c] != subStr[i]) {
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. bool checkDownRight (Words words , char *subStr , size_t row , size_t col){
  197. size_t len = strlen(subStr);
  198. for (size_t i = 0; i < len; i++) {
  199. size_t r = row + i;
  200. size_t c = col + i;
  201.  
  202. if (words.words[r][c] != subStr[i]) {
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. bool checkDownLeft (Words words , char *subStr , size_t row , size_t col){
  209. size_t len = strlen(subStr);
  210. for (size_t i = 0; i < len; i++) {
  211. size_t r = row + i;
  212. size_t c = col - i;
  213.  
  214. if (words.words[r][c] != subStr[i]) {
  215. return false;
  216. }
  217. }
  218. return true;
  219. }
  220.  
  221.  
  222. void findSubStr(Words words, char *subStr, int** usableWords , size_t lenOfWords, bool remove){
  223. size_t len,count;
  224. Direction dir;
  225. len = strlen(subStr);
  226. char leadingAlpha = subStr[0];
  227. count = 0;
  228. for (size_t row = 0; row < words.currentSize; ++row) {
  229. for (size_t col = 0; col < lenOfWords; ++col) {
  230. if (words.words[row][col] == leadingAlpha){
  231. dir.up = row+1 >= len ? true : false;
  232. dir.down = words.currentSize-row >= len ? true : false;
  233. dir.right = lenOfWords-col >= len ? true : false;
  234. dir.left = col+1 >= len ? true : false;
  235. dir.upLeft = dir.up && dir.left ? true : false;
  236. dir.upRight = dir.up && dir.right ? true : false;
  237. dir.downLeft = dir.down && dir.left ? true : false;
  238. dir.downRight = dir.down && dir.right ? true : false;
  239.  
  240. if (dir.up && checkUp(words,subStr,row,col)){
  241. if (remove){
  242. removeUp(strlen(subStr),row,col,usableWords);
  243. }
  244. count++;
  245. }
  246. if (dir.down && checkDown(words,subStr,row,col)) {
  247. if (remove){
  248. removeDown(strlen(subStr),row,col,usableWords);
  249. }
  250. count++;
  251. };
  252. if (dir.right && checkRight(words,subStr,row,col)) {
  253. if (remove){
  254. removeRight(strlen(subStr),row,col,usableWords);
  255. }
  256. count++;
  257. };
  258. if (dir.left && checkLeft(words,subStr,row,col)){
  259. if (remove){
  260. removeLeft(strlen(subStr),row,col,usableWords);
  261. }
  262. count++;
  263. };
  264. if (dir.upLeft && checkUpLeft(words,subStr,row,col)){
  265. if (remove){
  266. removeUpLeft(strlen(subStr),row,col,usableWords);
  267. }
  268. count++;
  269. };
  270. if (dir.upRight && checkUpRight(words,subStr,row,col)){
  271. if (remove){
  272. removeUpRight(strlen(subStr),row,col,usableWords);
  273. }
  274. count++;
  275. };
  276. if (dir.downLeft && checkDownLeft(words,subStr,row,col)) {
  277. if (remove){
  278. removeDownLeft(strlen(subStr),row,col,usableWords);
  279. }
  280. count++;
  281. };
  282. if (dir.downRight && checkDownRight(words,subStr,row,col)) {
  283. if (remove){
  284. removeDownRight(strlen(subStr),row,col,usableWords);
  285. }
  286. count++;
  287. };
  288. }
  289. }
  290. }
  291. printf("%s: %zux\n",subStr,count);
  292. }
  293.  
  294.  
  295. int main (void){
  296.  
  297. Words words;
  298. startALloc(&words);
  299. words.currentSize = 0;
  300.  
  301. size_t buffer;
  302. size_t lenOfWords = 0;
  303. size_t currentLen;
  304.  
  305. printf("Osmismerka:\n");
  306.  
  307. while ((currentLen = getline(&words.words[words.currentSize],&buffer,stdin)) != (size_t)EOF){
  308.  
  309. if (words.currentSize == 0){
  310. lenOfWords = currentLen;
  311. }
  312.  
  313. if (words.words[0][0] == '\n'){
  314. printf("Nespravny vstup.\n");
  315. free(words.words);
  316. return 0;
  317. }
  318.  
  319. if (words.currentSize > 0 && currentLen == 1 && words.words[words.currentSize][0] == '\n')
  320. break;
  321.  
  322. if (currentLen != lenOfWords){
  323. printf("Nespravny vstup.\n");
  324. free(words.words);
  325. return 0;
  326. }
  327. for (size_t i = 0; i < lenOfWords-1; ++i) {
  328. if ((words.words[words.currentSize][i] < 'a' || words.words[words.currentSize][i] > 'z') && words.words[words.currentSize][i] != '.'){
  329. printf("Nespravny vstup.\n");
  330. free(words.words);
  331. return 0;
  332. }
  333. }
  334. words.currentSize++;
  335. resize(&words);
  336. }
  337. int **usableWords = (int **) calloc(words.currentSize+10,sizeof (int*));
  338. for (size_t i = 0; i < words.currentSize; ++i) {
  339. usableWords[i] = (int *)calloc(lenOfWords+10,sizeof (int));
  340. }
  341. char* subStr = NULL;
  342. size_t mem;
  343. while (getline(&subStr,&mem,stdin) != EOF){
  344. if (subStr[0] == '?'){
  345. if (strlen(subStr) != 2){
  346. printf("Nespravny vstup.\n");
  347. free(words.words);
  348. return 0;
  349. }
  350. makeString(words,lenOfWords,usableWords);
  351. }
  352. else if (subStr[0] == '#'){
  353. bool remove = false;
  354. removeWhitespace(subStr);
  355. size_t lenOfSubStr = strlen(subStr);
  356. if (lenOfSubStr < 2){
  357. printf("Nespravny vstup.\n");
  358. free(words.words);
  359. return 0;
  360. }
  361. for (size_t i = 0; i < lenOfSubStr; ++i) {
  362. if (subStr[i] < 'a' || subStr[i] > 'z'){
  363. printf("Nespravny vstup.\n");
  364. free(words.words);
  365. return 0;
  366. }
  367. }
  368. findSubStr(words,subStr,usableWords,lenOfWords,remove);
  369. }
  370. else if (subStr[0] == '-'){
  371. bool remove = true;
  372. removeWhitespace(subStr);
  373. size_t lenOfSubStr = strlen(subStr);
  374. if (lenOfSubStr < 2){
  375. printf("Nespravny vstup.\n");
  376. free(words.words);
  377. return 0;
  378. }
  379. for (size_t i = 0; i < lenOfSubStr; ++i) {
  380. if (subStr[i] < 'a' || subStr[i] > 'z'){
  381. printf("Nespravny vstup.\n");
  382. free(words.words);
  383. return 0;
  384. }
  385. }
  386. findSubStr(words,subStr,usableWords,lenOfWords,remove);
  387. }
  388. else{
  389. printf("Nespravny vstup.\n");
  390. free(words.words);
  391. return 0;
  392. }
  393.  
  394. }
  395. free(words.words);
  396. return 0;
  397. }
  398.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Osmismerka: