题解 | #公共子串计算#

公共子串计算

http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

暴力搜索法事件复杂度O(n^3)空间复杂度o(1)

#include <algorithm>
#include <vector>


using namespace std;

int main() {
    string str1,str2;
    
    while(cin>>str1>>str2){
        int m =str1.size();
        int n =str2.size();
        int tempi,tempj,maxlen=-1,len=0;
        
        for(int i =0;i<m;i++){
            
            for(int j = 0;j<n;j++){
                tempi =i;
                tempj =j;
                while(str1[tempi]==str2[tempj]&&tempi<m&&tempj<n){
                    len++;
                    tempi++;
                    tempj++;
                }
                if(len>maxlen)maxlen=len;
                len=0;//重新记长度
                
            }
        }
        
        cout<<maxlen<<endl;
    }
}

全部评论
代码错的哦
点赞
送花
回复 分享
发布于 2022-04-04 22:06

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务