Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. string find_str_from2(string &s1, string &s2) {
  9.     int i = 0;
  10.     int j = 0;
  11.     int flag = 0;
  12.     int index = -1;
  13.     while (i < s1.length() && j < s2.length()) {
  14.         if (s1[i] == s2[j]){
  15.             if (flag == 0) {
  16.                 flag = 1;
  17.                 index = i;
  18.             }
  19.             else flag++;
  20.             i++;
  21.             j++;
  22.         }
  23.         else {
  24.             if (flag != 0) {
  25.                 j = 0;
  26.                 index = -1;
  27.                 flag = 0;
  28.             }
  29.             i++;
  30.         }
  31.     }
  32.     if (index == -1) return s1 + s2;
  33.     string str = "";
  34.     for (int k = 0; k < index; k++) {
  35.         str += s1[k];
  36.     }
  37.     for (int k = 0; k < s1.length() + s2.length() - flag - index; k++) {
  38.         str += s2[k];
  39.     }
  40.     return str;
  41. }
  42.  
  43. int find_str_from3(string &s1, string &s2, string &s3){
  44.     string str1 = find_str_from2(s1, s2);
  45.     string str2 = find_str_from2(str1, s3);
  46.     return str2.length();
  47. }
  48.  
  49. int main() {
  50.     ios_base::sync_with_stdio(false);
  51.     cin.tie(nullptr);
  52.     cout.tie(nullptr);
  53.  
  54.     string s1, s2, s3;
  55.     cin >> s1 >> s2 >> s3;
  56.  
  57.     if (s1 == s2 && s2 == s3) {
  58.         cout << s1.length();
  59.         return 0;
  60.     }
  61.  
  62.     if (s1 == s2) {
  63.         string str = find_str_from2(s1, s3);
  64.         cout << str.length();
  65.         return 0;
  66.     }
  67.     if (s2 == s3) {
  68.         string str = find_str_from2(s2, s1);
  69.         cout << str.length();
  70.         return 0;
  71.     }
  72.     if (s1 == s3) {
  73.         string str = find_str_from2(s1, s2);
  74.         cout << str.length();
  75.         return 0;
  76.     }
  77.  
  78.     int a = find_str_from3(s1, s2, s3);
  79.     int b = find_str_from3(s1, s3, s2);
  80.     int c = find_str_from3(s2, s1, s3);
  81.     int d = find_str_from3(s2, s3, s1);
  82.     int e = find_str_from3(s3, s1, s2);
  83.     int f = find_str_from3(s3, s2, s1);
  84.  
  85.     vector<int> vec = {a, b, c, d, e, f};
  86.     min_element(vec.begin(), vec.end());
  87.     cout << *min_element(vec.begin(), vec.end());
  88.    
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement