Advertisement
Guest User

12669_1

a guest
Nov 22nd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <ctype.h>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8. string T, P;
  9. int b[100010], n, m, numofoccurrence, maxdiff;
  10.  
  11. string flipcase(string s)
  12. {
  13.     string ret = "";
  14.     for (int i = 0; i < strlen(s.c_str()); i++)
  15.     {
  16.         ret += isupper(s[i]) ? tolower(s[i]) : islower(s[i]) ? toupper(s[i]) : s[i];
  17.     }
  18.     return ret;
  19. }
  20.  
  21. string convtolower(string s)
  22. {
  23.     string ret = "";
  24.     for (int i = 0; i < strlen(s.c_str()); i++)
  25.     {
  26.         ret += isupper(s[i]) ? tolower(s[i]) : s[i];
  27.  
  28.     }
  29.     return ret;
  30. }
  31.  
  32. int calculateDiff(string s1, string s2)
  33. {
  34.     int numofdiff = 0;
  35.     for (int i = 0; i < strlen(s1.c_str()); i++)
  36.     {
  37.         if (s1[i] != s2[i]) numofdiff += 1;
  38.     }
  39.     return numofdiff;
  40. }
  41.  
  42. void kmpPreprocess()
  43. {
  44.     int i = 0, j = -1; b[0] = -1;
  45.     while (i < m)
  46.     {
  47.         while (j >= 0 && (P[i] != P[j])) j = b[j];
  48.         i++; j++;
  49.         b[i] = j;
  50.     }
  51. }
  52.  
  53. void kmpSearch(string cursub, string cmain)
  54. {
  55.     int i = 0, j = 0;
  56.     while (i < n)
  57.     {
  58.         while (j >= 0 && (T[i] != P[j])) j = b[j];
  59.         i++; j++;
  60.         if (j == m)
  61.         {
  62.  
  63.             string occurstr = "";
  64.             numofoccurrence += 1;
  65.             int stindex = (i - j);
  66.             int edindex = (stindex + strlen(cursub.c_str()));
  67.             for (int x = stindex; x < edindex; x++)
  68.             {
  69.                 occurstr += cmain[x];
  70.  
  71.             }
  72.  
  73.  
  74.             int curdiff = calculateDiff(occurstr, cursub);
  75.             if (curdiff > maxdiff) maxdiff = curdiff;
  76.  
  77.             j = b[j];
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. int main()
  85. {
  86.     string origsub, origmain, sublower, mainlower;
  87.     int N, sublen, i, L, R, j;
  88.  
  89.  
  90.  
  91.  
  92.     while (cin>>N>>origsub)
  93.     {
  94.  
  95.  
  96.  
  97.  
  98.         cin>>origmain;
  99.         for (i = 0; i < N; i++)
  100.         {
  101.             cin>>L>>R;
  102.             string curmain = "";
  103.             for (j = (L - 1); j < R; j++)
  104.             {
  105.                 curmain += origmain[j];
  106.             }
  107.  
  108.             sublower = convtolower(origsub);
  109.             mainlower = convtolower(curmain);
  110.             T = mainlower; n = strlen(T.c_str());
  111.             P = sublower; m = strlen(P.c_str());
  112.             kmpPreprocess();
  113.             numofoccurrence = 0;
  114.             maxdiff = 0;
  115.             kmpSearch(origsub, curmain);
  116.             if (numofoccurrence == 0)
  117.             {
  118.                 printf("-1\n");
  119.             }
  120.             else
  121.             {
  122.                 printf("%d\n", maxdiff);
  123.             }
  124.  
  125.             string temp = "";
  126.             for (j = 0; j < (L - 1); j++)
  127.             {
  128.                 temp += origmain[j];
  129.             }
  130.  
  131.  
  132.             temp += flipcase(curmain);
  133.  
  134.  
  135.             for (j = R; j < strlen(origmain.c_str()); j++)
  136.             {
  137.                 temp += origmain[j];
  138.             }
  139.             origmain = temp;
  140.  
  141.         }
  142.  
  143.  
  144.  
  145.     }
  146.  
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement