由长度较短的字符串从长到短取所有子串,在另一个字符串中搜索,如果可以找到相等的字符串,则输出此字符串。
#include <iostream> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; if (s1.length() > s2.length()) { string temp = s1; s1 = s2; s2 = temp; } for (int i = s1.length(); i > 0; i--) { for (int j = 0; j <= s1.length() - i; j++) { string temp = s1.substr(j, i); if (s2.find(temp) != string::npos) { cout << temp; return 0; } } } cout << ""; return 0; } // 64 位输出请用 printf("%lld")