写出一个findDNAMatches函数,返回s1和s2两条DNA链匹配的位置
怎么做呢?用kmp算法应该怎么写?
除了要求写的函数以外的代码已经给出
#include <iostream>
#include <string>
#include "console.h"
using namespace std;
/* Prototypes */
int findDNAMatch(string s1, string s2, int start = 0);
string matchingStrand(string strand);
void findAllMatches(string s1, string s2);
void getC(string s,int C[]);
/* Main program */
int main() {
findAllMatches("TTGCC", "TAACGGTACGTC");
findAllMatches("TGC", "TAACGGTACGTC");
findAllMatches("CCC", "TAACGGTACGTC");
return 0;
}
/*
* Function: findAllMatches
* Usage: findAllMatches(s1, s2);
* ------------------------------
* Finds all positions at which s1 can bind to s2.
*/
void findAllMatches(string s1, string s2) {
int start = 0;
while (true) {
int index = findDNAMatch(s1, s2, start);
if (index == -1) break;
cout << s1 << " matches " << s2 << " at position " << index << endl;
start = index + 1;
}
if (start == 0) {
cout << s1 << " has no matches in " << s2 << endl;
}
}
/*
* Function: findDNAMatch
* Usage: int pos = findDNAMatch(s1, s2);
* int pos = findDNAMatch(s1, s2, start);
* ---------------------------------------------
* Returns the first index position at which strand s1 would bind to
* the strand s2, or -1 if no such position exists. If the start
* parameter is supplied, the search begins at that index position.
*/
int findDNAMatch(string s1, string s2, int start) {
// [TODO: modify and fill in the code]
return -1;
}