Advertisement
Infernale

Longest Common Substring [NCTU Floor 18]

Dec 25th, 2019
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void solving(string x, string y, int lx, int ly){
  9.     vector <string> res;
  10.     vector <vector <int>> mark(lx, vector <int>(ly));
  11.     int max = 0;
  12.     for(int i = 0; i < lx; i++){
  13.         for(int j = 0; j < ly; j++){
  14.             if(x[i] != y[j]){
  15.                 mark[i][j] = 0;
  16.                 continue;
  17.             }
  18.             if(i == 0 || j == 0){
  19.                 mark[i][j] = 1;
  20.             }else{
  21.                 mark[i][j] = mark[i - 1][j - 1] + 1;
  22.             }
  23.             if(mark[i][j] > max){
  24.                 max = mark[i][j];
  25.                 res.clear();
  26.                 res.push_back(x.substr(i + 1 - max, (i + 1) - (i + 1 - max)));
  27.             }else if(mark[i][j] == max){
  28.                 res.push_back(x.substr(i + 1 - max, (i + 1) - (i + 1 - max)));
  29.             }
  30.         }
  31.     }
  32.     sort(res.begin(), res.end());
  33.     cout << res[0] << endl;
  34. }
  35.  
  36. string evolve(string temp){
  37.     string a = temp;
  38.     a.erase(remove_if(a.begin(), a.end(), [](char c){
  39.         return c == ',' || c == '.' || c == '!' || c == '?';
  40.         }), a.end());
  41.     transform(a.begin(), a.end(), a.begin(), ::toupper);
  42.     return a;
  43. }
  44.  
  45. int main(){
  46.     string x, y;
  47.     cin >> x >> y;
  48.  
  49.     string X = evolve(x);
  50.     string Y = evolve(y);
  51.  
  52.     int m = x.length();
  53.     int n = y.length();
  54.  
  55.     solving(X, Y, m, n);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement