Maruf_Hasan

1255KMP

Oct 15th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. //In the Name of Allah Most Gracious, Most Merciful//
  2. /*If you want something you've never had, you have to do something you never did.*/
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. #define pb push_back
  9. #define ll long long
  10. #define pii pair<int,int>
  11. #define pll pair<ll,ll>
  12. #define M 1000007
  13. #define INF 1e9
  14. #define INFL 1e18
  15. #define PI acos(-1)
  16. #define mp make_pair
  17. #define MAX 100000
  18. //const int fx[]= {+1,-1,+0,+0};
  19. //const int fy[]= {+0,+0,+1,-1};
  20. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  21. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  22. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  23. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  24. int failure[1000010];
  25.  
  26.  
  27.  
  28. void build_failure_function(string pattern,int m)
  29. {
  30. failure[0]=0;
  31. failure[1]=0;
  32. for(int i=2;i<=m;i++)
  33. {
  34. //i is the length of the prefix we are dealing with
  35. //j is the index of the largest next partial match
  36. //(the largest suffix/prefix) of the string under index i-1
  37. int j=failure[i-1];
  38. while(true)
  39. {
  40.  
  41. if(pattern[j]==pattern[i-1])
  42. {
  43. // check the last character of prefix of length i expands the current candidate
  44. failure[i]=j+1;
  45. break;
  46. }
  47. if(j==0)
  48. {
  49. //if we cannot expand even the empty string
  50. failure[i]=0;
  51. break;
  52. }
  53. //else go to the next best candidate partial match
  54. j=failure[j];
  55.  
  56. }
  57. }
  58. }
  59.  
  60.  
  61. int kmp(string text,string pattern)
  62. {
  63. int n=text.size();
  64. int m=pattern.size();
  65. build_failure_function(pattern,m);
  66. int cnt=0;
  67. int i=0;//the initial state of automation is the empty string
  68. int j=0; //the first character of the text
  69. int spos=0;
  70. while(true)
  71. {
  72. if(j==n)
  73. {
  74. return cnt;
  75. }
  76. if(text[j]==pattern[i])
  77. {
  78. i++;
  79. j++;
  80. if(i==m)
  81. {
  82. // return true;
  83. j=j-m+1;
  84. i=0;
  85. cnt++;
  86. // break;
  87. }
  88.  
  89. }
  90. else
  91. {
  92. if(i==0)
  93. {
  94. //if we reached the empty string and failed to expand
  95. //even it ;we go to the
  96. //next character from the text,the state of automation
  97. //remains zero
  98. j++;
  99. }
  100. else
  101. {
  102. i=failure[i];
  103. }
  104. }
  105. }
  106. return cnt;
  107. }
  108.  
  109. int main()
  110. {
  111.  
  112. // #ifndef ONLINE_JUDGE
  113. // freopen("input.txt","r",stdin);
  114. // freopen("1255.txt","w",stdout);
  115. // #endif
  116. int t;
  117. int kase=0;
  118. scanf("%d ",&t);
  119. while(t--)
  120. {
  121.  
  122. string a,b;
  123. char aa[1000010];
  124. char bb[1000010];
  125. scanf("%s",aa);
  126. scanf("%s",bb);
  127. a=aa;
  128. b=bb;
  129. // cout<<a<<" "<<b<<endl;
  130. int ans=kmp(a,b);
  131. printf("Case %d: %d\n",kase+1,ans);
  132. kase++;
  133. // memset(failure,0,sizeof failure);
  134. }
  135. return 0;
  136. ///Before submit=>
  137. /// *check for integer overflow,array bounds
  138. /// *check for n=1
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment