Esempio n. 1
0
/**
 * Prints the longest common subsequence of two
 * long long int arrays to standard out. Easily
 * modified to return it in an array instead.
 * Approximate runtime: O(M*N)
 * Approximate space: O(M+N)
 * 
 */
void findLCS(long long int * A, int Abegin, int Aend, long long int * B, int Bbegin, int Bend){
    int Alen = Aend - Abegin;
    if (Alen == 0){
        return;
    }
    int Blen = Bend - Bbegin;
    long long int * newA = A;
    long long int * newB = B;
    newA += Abegin;
    newB += Bbegin;
    if ( Alen == 1){
		int i = 0;
        for(i = 0; i < Blen; i++){
            if(newA[0] == newB[i]){
            	//bingo, throw it to standard out
                printf("%lli ", newA[0]);
                break;
            }
        }
        return;
    } else {
        //Split A
        int AAbegin = Abegin;
        int ABbegin = Abegin + Alen/2;
        int AAend = Abegin + Alen/2;
        int ABend = Abegin + Alen;
        
        //Split B
        int BAbegin = Bbegin;
        int BBbegin = Bbegin + Blen/2;
        int BAend = Bbegin + Blen/2;
        int BBend = Bbegin + Blen;
        
        //Find correct split for B
        int sizeLCSOriginal = getLCSLength(A, Abegin, Aend, B, Bbegin, Bend);
		int k = 0;
        for (k = 0; k < Blen+1; k++){
            BBbegin = Bbegin + k;
            BAend = Bbegin + k;
            int sizeLCSA = getLCSLength(A, AAbegin, AAend, B, BAbegin, BAend);
            int sizeLCSB = getLCSLength(A, ABbegin, ABend, B, BBbegin, BBend);
            if(sizeLCSA + sizeLCSB == sizeLCSOriginal){
                break;
                //Found the split
            }
        }
        
        //Let's split!
        findLCS(A, AAbegin, AAend, B, BAbegin, BAend);
        findLCS(A, ABbegin, ABend, B, BBbegin, BBend);
        
        return;
    }
}
Esempio n. 2
0
/**
 * Takes the data from standard in,
 * outputs it to standard out. What more could you
 * ever ask for?
 * 
 * Not including constant factors:
 * Approx Runtime: O(M*N)
 * Approx Space: O(M+N)
 * Approx rekt: O(MLG)
 * Approx chance of hiring manager taking this lightly: 0%
 */
int main() {
    int n = 0; int m = 0;
    scanf("%i", &n); scanf("%i", &m);
    long long int A[n]; long long int B[m];
    int i = 0;
    for(i = 0; i < n; i++){
        scanf("%lli", &A[i]);
    }
    for(i = 0; i < m; i++){
        scanf("%lli", &B[i]);
    }
    findLCS(A, 0, n, B, 0, m);
    return 0;
}
Esempio n. 3
0
void test() {
    //std::cin >> sample >> text;
    std::string text = randomString(100, 3);
    std::string sample = randomString(100, 3);
    std::string answer = "";
    for (size_t i = 0; i < text.length(); ++i) {
        for (size_t j = 0; j < sample.length(); ++j) {
            std::string temp = "";
            size_t k = 0;
            while (i + k < text.length() && j + k < sample.length() && text[i + k] == sample[j + k]) {
                temp += text[i + k];
                ++k;
            }
            if (temp.length() > answer.length()) {
                answer = temp;
            }
        }
    }
    std::pair<std::pair<int, int>, int> hashAns = findLCS(sample, text);
    int firstStart = hashAns.first.first;
    int secondStart = hashAns.first.second;
    int leng = hashAns.second;
    std::string ansFirst = sample.substr(firstStart, leng);
    std::string ansSecond = text.substr(secondStart, leng);

    if (ansFirst != ansSecond || ansFirst.size() != answer.size()) {
        std::cout << "FAILED" << std::endl;
        std::cout << sample << std::endl;
        std::cout << text << std::endl;
        std::cout << "found: " << ansFirst << " and " << ansSecond << std::endl;
        std::cout << "expected: " << answer << std::endl;
        exit(1);
    } else {
        std::cout << "OK" << std::endl;
    }
}