用C++编写程序,输出两个字符串的最大公共子字符串

 

  

 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
原文地址:https://www.cnblogs.com/xiaovlxx/p/4506298.html