Submission details
Task:DNA sequence
Sender:hugues
Submission time:2016-10-22 15:05:23 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#10.35 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:51:60: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int k = 2; k <= MAX_K && k <= dna_sequence.length(); k++) {
                                                            ^

Code

#include <bits/stdc++.h>

using namespace std;

const int MAX_K = 10;
const double BASE = 7;
const long MODULO = 99999;

string YES = "YES";
string NO = "NO";

string hash_table[MAX_K + 1][MODULO];

long mod(long a, long b) { return ((a % b) + b) % b; }

long create_first_hash(string &s, int begin, int k) {
  //  cout << s.substr((unsigned long) begin, (unsigned long) k);
    long hash = 0;
    for (long i = k - 1, j = begin; i >= 0; i--, j++) {
        hash = mod(hash + (long) pow(BASE, i) * s[j], MODULO);
    }

   // cout << " " << hash << " ";
    return hash;
}

long create_next_hash(string &s, int begin, int k, long prev_hash) {
    //cout << s.substr((unsigned long) begin, (unsigned long) k);
    long hash = mod((long) (BASE * prev_hash + s[begin + k] - pow(BASE, k) * s[begin - 1]), MODULO);
    //cout << " " << hash << " ";
    return hash;
}

int main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);

    string dna_sequence;
    cin >> dna_sequence;

    int n;
    cin >> n;

    for (size_t i = 0; i < dna_sequence.length(); i++) {
        long hash = create_first_hash(dna_sequence, (int) i, 1);
        hash_table[1][hash] = YES;
    }

    // cout << endl;

    for (int k = 2; k <= MAX_K && k <= dna_sequence.length(); k++) {
        long hash = create_first_hash(dna_sequence, 0, k);
        hash_table[k][hash] = YES;

        for (size_t j = 1; j < dna_sequence.length() - k; j++) {
            hash = create_first_hash(dna_sequence, (int) j, k);
            hash_table[k][hash] = YES;
        }
    }

    string query;
    for (int i = 0; i < n; i++) {
        cin >> query;
        if (hash_table[query.length()][create_first_hash(query, 0, (int) query.length())] == YES) {
            cout << YES << endl;
        } else {
            cout << NO << endl;
        }
    }

    return 0;
}

Test details

Test 1

Verdict:

input
ACGCGGGCTCCTAGCGTTAGCAGTTGAGTG...

correct output
YES
YES
NO
NO
YES
...

user output
YES
YES
NO
YES
YES
...