簡體   English   中英

計算兩個給定 0 - 1 字符串的最長公共 substring 的長度

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

完整標題:

“計算兩個給定 0-1 字符串的最長公共子字符串的長度。輸入格式至少有兩個測試用例,每個測試用例由兩個長度最多為 100 的非空 0-1 字符串組成。輸入終止於EOF "

這是我的作業之一,我確實找到了計算兩個給定 0-1 字符串的最長公共子字符串長度的方法,但我不知道如何在其中輸入許多測試用例。
如果你們對這個問題有任何解決方案,請幫助我。

這是我的代碼:

#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