while True: try: s1=input() s2=input() if len(s1)>len(s2):#总体思路:从短的字符串中取子串,看其在长字符串中是否存在 s1,s2=s2,s1 length=0 for i in range(len(s1)): for j in range(i+1,len(s1)): sub=s1[i:j] if sub in s2 and j-i>length: res=sub length=j-i print(res) except: break
#和python的解法本质上是一样的,不过细节上要注意,比如substr()函数第二个参数是子串的长度 #include <iostream> #include <string> using namespace std; int main(){ string s1,s2,temp; while(cin >> s1 >> s2){ if(s1.size() > s2.size()) swap(s1,s2); string sub,res; int length=0; for(int i=0;i<s1.size();i++){ for(int j=i+1;j<s1.size();j++){ sub=s1.substr(i,j-i+1); if(s2.find(sub)==-1) break; else if(sub.size()>res.size()) res=sub; } } cout << res << endl; } return 0; }