传送门
题目描述
小X身为奆老,兴趣爱好广泛,他还非常喜欢研究DNA序列……
小X进行了一项关于DNA序列研究,发现人某条染色体上的一段DNA序列中连续的k个碱基组成的碱基序列与做题的AC率有关!于是他想研究一下这种关系。
现在给出一段DNA序列,请帮他求出这段DNA序列中所有连续k个碱基形成的碱基序列中,出现最多的一种的出现次数。
输入
第一行为一段DNA序列,保证DNA序列合法,即只含有A,G,C,T四种碱基;
第二行为一个正整数k,意义与题目描述相同。
输出
一行,一个正整数,为题目描述中所求答案。
哈希一两次即可
#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const int N = 5e6 + 100;
ull Hash[N], p[N];
string s;
int len;
unordered_map<ull, int> mp;
void _hash() {
Hash[0] = 0;
for (int i = 1; i <= len; i++) {
Hash[i] = Hash[i - 1] * 131 + s[i] - '0' + 1;
}
p[0] = 1;
for (int i = 1; i <= len; i++)
p[i] = p[i - 1] * 131;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k;
cin >> s >> k;
len = s.size();
s = " " + s;
_hash();
for (int i = 1; i + k - 1 <= len; i++) {
int l = i, r = i + k - 1;
mp[Hash[r] - Hash[l - 1] * p[r - l + 1]]++;
}
int ans = -1;
for (unordered_map<ull, int>::iterator it = mp.begin(); it != mp.end(); it++) {
ans = max(ans, it->second);
}
cout << ans << endl;
return 0;
}