Guest User

Untitled

a guest
Nov 23rd, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void GetLargestCommonSubstring(const char a[], const char [] b, char res[]) {
  6.     const int a_size = a.size();
  7.     const int b_size = b.size();
  8.    
  9.     typedef vector<int> solution;
  10.    
  11.     const int solution_size = b_size + 1;
  12.     solution x(solution_size, 0), y(solution_size);
  13.    
  14.     solution * previous = &x;
  15.     solution * current = &y;
  16.    
  17.     int max_length = 0;
  18.     int result_index = 0;
  19.    
  20.     for(int i = a_size - 1; i >= 0; i--) {
  21.         for(int j = b_size - 1; j >= 0; j--) {
  22.             int & current_match = (*current)[j];
  23.             if(a[i] != b[j]) {
  24.                 current_match = 0;
  25.             }
  26.             else {
  27.                 const int length = 1 + (*previous)[j + 1];
  28.                 if (length > max_length) {
  29.                     max_length = length;
  30.                     result_index = i;
  31.                 }
  32.                
  33.                 current_match = length;
  34.             }
  35.         }
  36.        
  37.         swap(previous, current);
  38.     }
  39.    
  40.     result = a.substr(result_index, max_length);
  41. }
  42.  
  43.  
  44. int main(){
  45.     char result[100];
  46.  
  47.     const char str1[] = "djhgkjfdjklgjdfjkglasd1234";
  48.     const char str2[] = "lgjdf39485093849058jklkdjf";
  49.     GetLargestCommonSubstring(str1, str2,result);
  50.     cout<<result;
  51.    
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment