原题连接: 【满分】【华为OD机试真题2023 JAVA】查找重复代码_若博豆的博客-CSDN博客
c++代码:
#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;
}
时间复杂度: O(n)
因为字符串100长度, 所以可以暴力枚举
双指针, 时间复杂度 n * n