【编程题】最长公共连续子串
2017-05-18 19:37
183 查看
牛牛有两个字符串(可能包含空格),牛牛想找出其中最长的公共连续子串,希望你能帮助他,并输出其长度。
输入描述:
输出描述:
输入例子:
输出例子:
输入描述:
输入为两行字符串(可能包含空格),长度均小于等于50.
输出描述:
输出为一个整数,表示最长公共连续子串的长度。
输入例子:
abcde abgde
输出例子:
2
#include <iostream> #include <string.h> #include <vector> using namespace std; int main() { string str1; string str2; getline(cin,str1); getline(cin,str2); int len1 = str1.size(); int len2 = str2.size(); if(len1<1 || len2<1) { cout<<0<<endl; return 0; } vector<vector<int>> dp(len1,vector<int>(len2,0)); for(int ii = 0;ii<len2;++ii) { if(str1[0] ==str2[ii]) dp[0][ii] = 1; } for(int jj = 0;jj<len1;++jj) { if(str1[jj] ==str2[0]) dp[jj][0] = 1; } for(int ii = 1;ii<len1;++ii) { for(int jj = 1;jj<len2;++jj) { if(str1[ii] ==str2[jj]) dp[ii][jj] = dp[ii-1][jj-1]+1; } } int longest = 0; for(int ii = 0;ii<len1;++ii) { for(int jj = 0;jj<len2;++jj) { if(dp[ii][jj] >longest) longest = dp[ii][jj]; } } cout<<longest<<endl; }
相关文章推荐
- [编程题] 最长公共连续子串
- 美团2017编程题—拼凑钱币&大富翁游戏&最大矩形面积&最长公共连续子串
- 牛客网[编程题] 最长公共连续子串
- [编程题] 最长公共连续子串(美团点评2017秋招)
- 最长公共子串(连续)问题
- [编程题]最长公共子串
- 两个字符串的最长连续公共子串
- 最长连续公共子串
- LCP数组的实现和最长公共连续子串
- 求两字符串的最长公共连续子串
- LCS(最长公共子序列)注意:是可以不连续的,区别于最长公共子串
- 最长公共连续子串
- 求两字符串最长公共连续子串---C++编程
- 最长公共子串与最长连续子串算法
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- 字符串题目-最长的公共连续子串
- 动态规划-----最长公共连续子串
- 最长连续公共子串算法
- POJ 3080:Blue Jeans:枚举求解n个字符串的最长公共连续子串
- 最长连续公共子串算法