Original question link: [Full marks][Huawei OD machine test real questions 2023 JAVA] Find duplicate codes_Ruo Bodou's blog-CSDN blog
c++ code:
#include <iostream>
using namespace std;
int main()
{
string a;//0-200 暴力枚举
string b;
getline(cin, a);
getline(cin, b);
int len1 = a.length();
int len2 = b.length();
if(len1 > len2) swap(a, b);//让a是短的
string res = "";
//双指针 [i,j] 判断 n * n = 10000
for(int j = len1 - 1; j > 0; j -- )
{
for(int i = 0; i < j; i ++ )
{
string temp = a.substr(i, j - i + 1);
if(b.find(temp) != -1) {
if(temp.length() > res.length())
res = temp;
}
}
}
cout << res << endl;
return 0;
}
Time complexity: O(n)
Because the string is 100 in length, it can be violently enumerated
Double pointers, time complexity n * n