用C++編寫程序,輸出兩個字符串的最大公共子字符串,編寫程序字符串
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112581885.gif)
![]()
1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6 string s_l,s_sh;
7 cin>>s_l>>s_sh;
8 if(s_l.size()<s_sh.size())
9 {
10 string s0;
11 s0=s_l;
12 s_l=s_sh;
13 s_sh=s0;
14 }
15
16 int len=s_sh.size();
17 string s;
18 int finds=0;
19
20 for(int i=len;i>0;i--)
21 {
22 for(int j=0;j<len-1;j++)
23 {
24 if(i+j<=len)
25 {
26 s=s_sh.substr(j,i);
27 if(s_l.find(s)!=-1)
28 {
29 finds=1;
30 break;
31 }
32
33 }
34 }
35 if(finds==1)
36 break;
37 }
38
39 cout<<s<<endl;
40
41 return 0;
42 }
View Code