简体   繁体   English

计算两个给定 0 - 1 字符串的最长公共 substring 的长度

[英]Compute the length of a longest common substring of two given 0 - 1 strings

Full title:完整标题:

"Compute the length of a longest common sub-string of two given 0 - 1 strings. Input format has at least two test cases, each consisting of two non-empty 0-1 strings of lengths at most 100.The input terminates on EOF " “计算两个给定 0-1 字符串的最长公共子字符串的长度。输入格式至少有两个测试用例,每个测试用例由两个长度最多为 100 的非空 0-1 字符串组成。输入终止于EOF "

Here is one of my Homework, I did found out the way to compute the length of a longest common sub-string of two given 0-1 strings but I don't know how to input many test cases at ones.这是我的作业之一,我确实找到了计算两个给定 0-1 字符串的最长公共子字符串长度的方法,但我不知道如何在其中输入许多测试用例。
Please help me if you guys have any solution for this problem.如果你们对这个问题有任何解决方案,请帮助我。

This is my code:这是我的代码:

#include <string> 
using namespace std; 
string A,B; 
int lcs(int i, int j, int count) 
{ 
if (i == 0 || j == 0) 
return count; 
if (A[i-1] == B[j-1]) 
{ 
count = lcs(i - 1, j - 1, count + 1); 
}
count = max(count, max(lcs( i, j - 1, 0), lcs( i - 1, j, 0))); 
return count; 
}

int main() 
{ 
int n,m; 
cout << "Input String A and B \n"; 
cin >> A; cin >> B; 
n=A.size(); 
m=B.size(); 
cout<< "Longest common substring "<< lcs(n,m,0) << endl; 
return 0; 
} 
int solveYourProblemFunc(string str1, string str2) {
/* your code */
}

int main() {
   int testCount;
   cin >> testCount;
   vector<int> results;
   while(testCount--) {
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);
    int res = solveYourProblemFunc();
    results.push_back(res);
   }
  /* output results  */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM