second algo - final
This commit is contained in:
parent
33ddebed69
commit
924693f2c9
5 changed files with 104 additions and 10 deletions
|
@ -1,7 +1,7 @@
|
|||
CC=gcc
|
||||
CFLAGS=-Wall -lm -Wall -Werror=sign-compare -Werror=array-bounds -Werror=maybe-uninitialized -Werror=unused-parameter -Werror=maybe-uninitialized -Werror=cast-qual
|
||||
|
||||
EXECS=knight
|
||||
EXECS=knight queens rabin_karp
|
||||
|
||||
build_knight: knight.c
|
||||
$(CC) $(CFLAGS) knight.c -o knight
|
||||
|
@ -12,5 +12,11 @@ build_queens: queens.c
|
|||
$(CC) $(CFLAGS) queens.c -o queens
|
||||
queens_run: queens
|
||||
./queens
|
||||
|
||||
build_rabin_karp: rabin_karp.c
|
||||
$(CC) $(CFLAGS) rabin_karp.c -o rabin_karp
|
||||
rabin_karp_run: rabin_karp
|
||||
./rabin_karp
|
||||
|
||||
clean:
|
||||
rm -f $(EXECS)
|
||||
|
|
|
@ -103,7 +103,7 @@ int sum_of_moves(int **array, int N, int x1, int y1) {
|
|||
}
|
||||
return sum;
|
||||
}
|
||||
/* Сумма элементов массива должна быть равна сумме чисел от 1 до N (т.е. обошли всю доску) */
|
||||
/* Сумма элементов массива должна быть равна сумме чисел от 1 до N*N (т.е. обошли всю доску) */
|
||||
int sum_check(int **array, int N) {
|
||||
long long unsigned sum = 0;
|
||||
long long unsigned total_sum = 0;
|
||||
|
@ -142,16 +142,13 @@ int check(int **array, int N, int x, int y) {
|
|||
if (sum_check(array, N) && is_looped(array, N)) {
|
||||
int difference = array[x][y] - 1;
|
||||
/* Изменяем ходы, будто мы ходили с другого поля */
|
||||
for (int x2 = 0; x2 < N; x2++) {
|
||||
for (int x2 = 0; x2 < N; x2++)
|
||||
for (int y2 = 0; y2 < N; y2++) {
|
||||
if (array[x2][y2] > difference) {
|
||||
if (array[x2][y2] > difference)
|
||||
array[x2][y2] -= difference;
|
||||
}
|
||||
else {
|
||||
else
|
||||
array[x2][y2] = N * N + array[x2][y2] - difference;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
/* Задача о ферзях */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define _DEBUG
|
||||
|
||||
/* Глобальная переменная для количества решений, удобнее, чем таскать указатель на счетчик по рекурсии */
|
||||
int SOLUTIONS = 0;
|
||||
|
@ -35,7 +34,7 @@ int main() {
|
|||
lines = (int*)calloc(N, sizeof(int));
|
||||
|
||||
solve(chessboard, lines, N);
|
||||
printf("%d", SOLUTIONS);
|
||||
printf("%d\n", SOLUTIONS);
|
||||
|
||||
delete_array(chessboard, N);
|
||||
free(lines);
|
||||
|
|
76
algo/second/rabin_karp.c
Normal file
76
algo/second/rabin_karp.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#define POW 12
|
||||
|
||||
long long unsigned hash(char* string);
|
||||
void search_pattern_in_text(char* FILE_WAY, char* STRING);
|
||||
void get_string(char* string, long long unsigned len);
|
||||
|
||||
int main() {
|
||||
char *FILE_PATH = "rabin_karp.data";
|
||||
char* STRING = "Let";
|
||||
search_pattern_in_text(FILE_PATH, STRING);
|
||||
}
|
||||
|
||||
long long unsigned hash(char* string) {
|
||||
long long unsigned len_string = strlen(string);
|
||||
long long unsigned result = 0;
|
||||
for (long long unsigned i = 0; i < len_string; i++) {
|
||||
result = (result * POW + string[i]) % ULONG_MAX;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
void search_pattern_in_text(char *FILE_PATH, char *string) {
|
||||
freopen(FILE_PATH, "r", stdin);
|
||||
|
||||
int c;
|
||||
long long unsigned string_len = strlen(string);
|
||||
long long unsigned string_hash = hash(string);
|
||||
|
||||
/* "Текущая" подстрока, её мы будем смещать по тексту */
|
||||
char *string_cur = (char*)calloc(string_len + 1, sizeof(char));
|
||||
get_string(string_cur, string_len);
|
||||
long long unsigned string_cur_hash = hash(string_cur);
|
||||
|
||||
long long unsigned MAX_POW = 1;
|
||||
/* Получаем максимальную степень для элемента строки */
|
||||
for (long long unsigned i = 1; i < string_len; i++) {
|
||||
MAX_POW = (MAX_POW * POW) % ULONG_MAX;
|
||||
}
|
||||
|
||||
for (long long unsigned i = 0; (c = getchar()) != EOF; i++) {
|
||||
/* Проверяем совпадение подстроки посимвольно, чтоб избежать коллизий */
|
||||
if (string_hash == string_cur_hash) {
|
||||
if (!strcmp(string, string_cur)) {
|
||||
printf("%llu\n", i);
|
||||
}
|
||||
}
|
||||
/* Обновляем хеш строки (для следующей подстроки) */
|
||||
string_cur_hash = ((string_cur_hash - string_cur[0]*MAX_POW)*POW + c) % ULONG_MAX;
|
||||
|
||||
/* "Смещаем" подстроку на один символ дальше */
|
||||
for (long long unsigned j = 0; j < string_len - 1; j++) {
|
||||
string_cur[j] = string_cur[j + 1];
|
||||
}
|
||||
string_cur[string_len - 1] = c;
|
||||
|
||||
}
|
||||
free(string_cur);
|
||||
|
||||
fclose(stdin);
|
||||
}
|
||||
/* Считываем только n символов */
|
||||
void get_string(char* string, long long unsigned n) {
|
||||
int c;
|
||||
for (long long unsigned i = 0; i < n; i++) {
|
||||
if ((c = getchar()) != EOF)
|
||||
string[i] = c;
|
||||
else
|
||||
assert(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
16
algo/second/rabin_karp.data
Normal file
16
algo/second/rabin_karp.data
Normal file
|
@ -0,0 +1,16 @@
|
|||
Let me Let tell you about mv best friend.
|
||||
His name is Yuriy. We have known each other for ages.
|
||||
We live in the same town and went to the same school.
|
||||
Now Let we study at the same university And though we study at different faculties, we see each other almost every day.
|
||||
My best friend is the first to come and support me in any difficult situation.
|
||||
We have a lot in common. We both do sports regularly.
|
||||
That's because we want to be strong and look handsome.
|
||||
We really look very much alike. We have short dark hair, grey eyes and a sport figure.
|
||||
We also have many similar features of character: we are merry, smart and active.
|
||||
|
||||
As Yura is an easy going person, he can easily make friends with anyone He likes communicating with new people.
|
||||
Yura likes travelling. We often visit new places, especially in summer.
|
||||
We have already been to the Crimea, Poltava and Lvov. This year we plan to visit the Carpathian Mountains.
|
||||
Yura's hobby is computer games. He can spend hours in the virtual world.
|
||||
As for me, I like the Internet, where I find interesting information and make friends with people all over the world.
|
||||
But my best friend lives in Ukraine. I am glad I have such a friend as Yura.
|
Reference in a new issue