fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define IMX INT_MAX
  5. #define IMN INT_MIN
  6. int discard; //you can access string length using the function
  7. string str1, str2, temp;
  8. int main () {
  9. cin >> discard >> discard >> str1 >> str2;
  10. if (str1.length() > str2.length()) {//if str1 is not the shorter one, make it the shorter one by switching
  11. temp = str2;
  12. str2 = str1;
  13. str1 = temp;
  14. }
  15. for (int size = str1.length(); size >= 1; size--) { //backwards length checking
  16. for (int j = 0; j <= str1.length() - size; j++) { //start position
  17. temp = str1.substr(j, size); //the substring
  18. if (str2.find(temp) != string::npos) { //if the substring works, it is the largest
  19. cout << "The longest common substring: " << temp; //output and end program
  20. return 0;
  21. }
  22. }
  23. }
  24. cout << "No Common Substring";
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5480KB
stdin
17 60
KXCGMTMVVGFQQWSPD
JXZADDUKVLQPKUZJZHWSUTPCAFSYAIBJHAMMEGWBTPQELRNKBLDXGUZGCSEC
stdout
The longest common substring: WS