题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
#include <iostream> using namespace std; int main(){ string ls,ss; while (cin>>ls>>ss){ if (ls.size()<ss.size()) swap(ls,ss); int n = ss.size(); bool flag = false; while (n--){ if (flag) break; for (int i = 0; i <= ss.size()-n; ++i) { string tem = ss.substr(i,n); if (ls.find(tem) != string::npos){ cout<<tem<<endl; flag = true; break; } } } } return 0; }