Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #ifdef _MSC_VER
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. int Plength = 1000000;
  9. char P[1000001];
  10. char *text_r1;
  11. char *text_r2;
  12. char *text_r3;
  13.  
  14. char *sub_r1;
  15. char *sub_r2;
  16. char *sub_r3;
  17.  
  18. char *search;
  19.  
  20. void generateP() {
  21.     P[0] = 'a';
  22.     long read = 0, write = 1;
  23.     while (Plength>write) {
  24.         if (read % 39 == 38) read++;
  25.         char r = P[read++];
  26.         if (r == 'a') { P[write++] = 'b';P[write++] = 'a';P[write++] = 'a'; }
  27.         else if (r == 'b') { P[write++] = 'a';P[write++] = 'c'; }
  28.         else if (r == 'c') { P[write++] = 'b'; };
  29.     }
  30.     P[Plength] = 0;
  31. }
  32.  
  33. int *kmp_set(char *sub, int n) {
  34.  
  35.     int *t = (int*)malloc(n * sizeof(int));
  36.     int i = 0;
  37.     int j = 1;
  38.     t[0] = 0;
  39.     char temp = 0;
  40.  
  41.     while (j < n) {
  42.         if (sub[i] != sub[j]) {
  43.             if (i == 0 && j == n - 1) {
  44.                 t[j] = 0;
  45.                 break;
  46.             }
  47.             else if (i == 0 && j != n - 1) {
  48.                 t[j] = 0;
  49.                 j++;
  50.                 continue;
  51.             }
  52.             i = t[i - 1];
  53.         }
  54.         else if (sub[i] == sub[j]) {
  55.             t[j] = ++i;
  56.             j++;
  57.         }
  58.     }
  59.     return t;
  60. }
  61.  
  62. char *kmp(char *sub, char *text, char sub_s, char text_s) {
  63.  
  64.     int *seq = kmp_set(sub, sub_s);
  65.     char *result = (char*)malloc(text_s * sizeof(char));
  66.  
  67.     for (int i = 0; i < text_s; i++) {
  68.         result[i] = 0;
  69.     }
  70.  
  71.     if (text_s < sub_s)
  72.         return result;
  73.  
  74.     int text_i = 0;
  75.     int sub_i = 0;
  76.  
  77.     while (text_i < text_s) {
  78.         if (text[text_i] == sub[sub_i]) {
  79.             if (sub_i == sub_s - 1) {
  80.                 //return text_i - sub_s + 1;
  81.                 result[text_i - sub_s + 1] = 1;
  82.                 sub_i = 0;
  83.  
  84.                 if (sub_s == 1)
  85.                     text_i++;
  86.                 continue;
  87.             }
  88.             text_i++;
  89.             sub_i++;
  90.         }
  91.         else {
  92.             if (sub_i == 0) {
  93.                 text_i++;
  94.             }
  95.             else {
  96.                 sub_i = seq[sub_i - 1];
  97.             }
  98.         }
  99.     }
  100.  
  101.     return result;
  102. }
  103.  
  104. int find(int text_s, int sub_s, int t_i, int s_i, int count) {
  105.    
  106.     if (t_i > text_s)
  107.         return -1;
  108.  
  109.     if (text_r1[t_i] && sub_r1[s_i]) {
  110.         if (s_i + 1 == sub_s)
  111.             return count + 1;
  112.         return find(text_s, sub_s, t_i + 9, s_i + 1, count + 1);
  113.     }
  114.     if (text_r2[t_i] && sub_r2[s_i]) {
  115.         if (s_i + 5 == sub_s)
  116.             return count + 1;
  117.         return find(text_s, sub_s, t_i + 4, s_i + 5, count + 1);
  118.     }
  119.     if (text_r3[t_i] && sub_r3[s_i]) {
  120.         if (s_i + 3 == sub_s) {
  121.             return count + 1;
  122.  
  123.         }
  124.         return find(text_s, sub_s, t_i + 2, s_i + 3, count + 1);
  125.     }
  126.     if (P[t_i] == search[s_i]) {
  127.         if (s_i + 1 == sub_s)
  128.             return count;
  129.         return find(text_s, sub_s, t_i + 1, s_i + 1, count);
  130.     }
  131.     return -1;
  132.  
  133. }
  134.  
  135.  
  136. void solve(int text_s, int sub_s) {
  137.  
  138.     /*
  139.     Pravidlo 1: aa->aaa
  140.     Pravidlo 2: abac->abaac
  141.     Pravidlo 3: baabaabaa->a
  142.     */
  143.  
  144.     text_r1 = kmp("baabaabaa", P, 9, text_s);
  145.     text_r2 = kmp("abac", P, 4, text_s);
  146.     text_r3 = kmp("aa", P, 2, text_s);
  147.  
  148.     sub_r1 = kmp("a", search, 1, sub_s);
  149.     sub_r2 = kmp("abaac", search, 5, sub_s);
  150.     sub_r3 = kmp("aaa", search, 3, sub_s);
  151.  
  152.     int min_i = -1;
  153.     //int min_c = INT_MAX;
  154.     int min_c = 2147483647;
  155.     int local_min;
  156.     for (int i = 0; i < text_s; i++) {
  157.         local_min = find(text_s, sub_s, i, 0, 0);
  158.  
  159.         if (local_min != -1 && local_min < min_c) {
  160.             min_c = local_min;
  161.             min_i = i;
  162.         }
  163.     }
  164.  
  165.     if (min_c != 2147483647)
  166.         printf("%d,%d\n", min_i, min_c);
  167.     else
  168.         printf("No solution.\n");
  169.  
  170. }
  171.  
  172. int main() {
  173.  
  174.     generateP();
  175.  
  176.     int limit, input_size;
  177.     search = (char*)malloc(1000 * sizeof(char));
  178.  
  179.     //scanf("%d", &input_size);
  180.     input_size = 1;
  181.     for (int i = 0; i < input_size; i++) {
  182.         scanf("%d %s", &limit, search);
  183.         getchar();
  184.         solve(limit, strlen(search));
  185.     }
  186.     printf("\n");
  187.     getchar();
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement