Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- int Plength = 1000000;
- char P[1000001];
- char *text_r1;
- char *text_r2;
- char *text_r3;
- char *sub_r1;
- char *sub_r2;
- char *sub_r3;
- char *search;
- void generateP() {
- P[0] = 'a';
- long read = 0, write = 1;
- while (Plength>write) {
- if (read % 39 == 38) read++;
- char r = P[read++];
- if (r == 'a') { P[write++] = 'b';P[write++] = 'a';P[write++] = 'a'; }
- else if (r == 'b') { P[write++] = 'a';P[write++] = 'c'; }
- else if (r == 'c') { P[write++] = 'b'; };
- }
- P[Plength] = 0;
- }
- int *kmp_set(char *sub, int n) {
- int *t = (int*)malloc(n * sizeof(int));
- int i = 0;
- int j = 1;
- t[0] = 0;
- char temp = 0;
- while (j < n) {
- if (sub[i] != sub[j]) {
- if (i == 0 && j == n - 1) {
- t[j] = 0;
- break;
- }
- else if (i == 0 && j != n - 1) {
- t[j] = 0;
- j++;
- continue;
- }
- i = t[i - 1];
- }
- else if (sub[i] == sub[j]) {
- t[j] = ++i;
- j++;
- }
- }
- return t;
- }
- char *kmp(char *sub, char *text, char sub_s, char text_s) {
- int *seq = kmp_set(sub, sub_s);
- char *result = (char*)malloc(text_s * sizeof(char));
- for (int i = 0; i < text_s; i++) {
- result[i] = 0;
- }
- if (text_s < sub_s)
- return result;
- int text_i = 0;
- int sub_i = 0;
- while (text_i < text_s) {
- if (text[text_i] == sub[sub_i]) {
- if (sub_i == sub_s - 1) {
- //return text_i - sub_s + 1;
- result[text_i - sub_s + 1] = 1;
- sub_i = 0;
- if (sub_s == 1)
- text_i++;
- continue;
- }
- text_i++;
- sub_i++;
- }
- else {
- if (sub_i == 0) {
- text_i++;
- }
- else {
- sub_i = seq[sub_i - 1];
- }
- }
- }
- return result;
- }
- int find(int text_s, int sub_s, int t_i, int s_i, int count) {
- if (t_i > text_s)
- return -1;
- if (text_r1[t_i] && sub_r1[s_i]) {
- if (s_i + 1 == sub_s)
- return count + 1;
- return find(text_s, sub_s, t_i + 9, s_i + 1, count + 1);
- }
- if (text_r2[t_i] && sub_r2[s_i]) {
- if (s_i + 5 == sub_s)
- return count + 1;
- return find(text_s, sub_s, t_i + 4, s_i + 5, count + 1);
- }
- if (text_r3[t_i] && sub_r3[s_i]) {
- if (s_i + 3 == sub_s) {
- return count + 1;
- }
- return find(text_s, sub_s, t_i + 2, s_i + 3, count + 1);
- }
- if (P[t_i] == search[s_i]) {
- if (s_i + 1 == sub_s)
- return count;
- return find(text_s, sub_s, t_i + 1, s_i + 1, count);
- }
- return -1;
- }
- void solve(int text_s, int sub_s) {
- /*
- Pravidlo 1: aa->aaa
- Pravidlo 2: abac->abaac
- Pravidlo 3: baabaabaa->a
- */
- text_r1 = kmp("baabaabaa", P, 9, text_s);
- text_r2 = kmp("abac", P, 4, text_s);
- text_r3 = kmp("aa", P, 2, text_s);
- sub_r1 = kmp("a", search, 1, sub_s);
- sub_r2 = kmp("abaac", search, 5, sub_s);
- sub_r3 = kmp("aaa", search, 3, sub_s);
- int min_i = -1;
- //int min_c = INT_MAX;
- int min_c = 2147483647;
- int local_min;
- for (int i = 0; i < text_s; i++) {
- local_min = find(text_s, sub_s, i, 0, 0);
- if (local_min != -1 && local_min < min_c) {
- min_c = local_min;
- min_i = i;
- }
- }
- if (min_c != 2147483647)
- printf("%d,%d\n", min_i, min_c);
- else
- printf("No solution.\n");
- }
- int main() {
- generateP();
- int limit, input_size;
- search = (char*)malloc(1000 * sizeof(char));
- //scanf("%d", &input_size);
- input_size = 1;
- for (int i = 0; i < input_size; i++) {
- scanf("%d %s", &limit, search);
- getchar();
- solve(limit, strlen(search));
- }
- printf("\n");
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement