• 187. 重复的DNA序列


    链接:

    187. 重复的DNA序列

    题解:

    1. class Solution {
    2. using ll = unsigned long long;
    3. ll P = 13331; // 取131会有哈稀冲突
    4. public:
    5. vector findRepeatedDnaSequences(string s) {
    6. int n = s.size();
    7. vector f(n+1, 0), p(n+1, 0); p[0] = 1;
    8. for(int i=0; i
    9. f[i+1] = f[i] * P + s[i];
    10. p[i+1] = p[i] * P;
    11. }
    12. unordered_mapint> d;
    13. vector ret;
    14. for(int i=9; i// 枚举右端点
    15. int h = f[i+1] - f[i-9] * p[10];
    16. if(++d[h] == 2) // 第二次出现
    17. ret.push_back(s.substr(i-9, 10));
    18. }
    19. return ret;
    20. }
    21. };
    1. class Solution {
    2. const int L = 10;
    3. public:
    4. vector findRepeatedDnaSequences(string s) {
    5. vector ans;
    6. unordered_mapint> cnt;
    7. int n = s.length();
    8. for (int i = 0; i <= n - L; ++i) {
    9. string sub = s.substr(i, L);
    10. if (++cnt[sub] == 2) {
    11. ans.push_back(sub);
    12. }
    13. }
    14. return ans;
    15. }
    16. };

    下面这两种通过不了:

    1. class Solution {
    2. public:
    3. vector findRepeatedDnaSequences(string s) {
    4. if (s.size() < 10) {
    5. return {};
    6. }
    7. long ten9 = 1e9 + 7;
    8. long hhash = 1;
    9. long B = 256;
    10. //std::unordered_map table{{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};
    11. for (int i = 0; i < 9; ++i) {
    12. hhash = (hhash * B + s[i]) % ten9;
    13. //hhash = (hhash * B + table[s[i]]) % ten9;
    14. }
    15. long P = 1;
    16. for (int i = 0; i < 9; ++i) {
    17. P = P * B % ten9;
    18. }
    19. std::unordered_map<long, int> hash2index; // hash值对应的最开始字符
    20. //cout << hhash << endl;
    21. //hash2index[hhash] = 0;
    22. std::vector result;
    23. for (int i = 9; i < s.size(); ++i) {
    24. hhash = (hhash * B + s[i]) % ten9;
    25. //long prev = (hhash - table[s[i-10]] * P) % ten9 * B;
    26. //hhash = (prev + table[s[i]] * B % ten9) % ten9;
    27. //cout << "i = " << i << " prev = " << prev << " new = " << hhash << endl;
    28. cout << "i = " << i << " hhash = " << hhash << endl;
    29. if (hash2index.find(hhash) != hash2index.end()) {
    30. result.push_back(s.substr(i-9, 10));
    31. } else {
    32. hash2index[hhash] = i - 9;
    33. }
    34. hhash = (hhash - s[i-9] * P % ten9 + ten9) % ten9;
    35. }
    36. return result;
    37. }
    38. };
    1. class Solution {
    2. public:
    3. vector findRepeatedDnaSequences(string s) {
    4. if (s.size() < 10) {
    5. return {};
    6. }
    7. long ten9 = 1e9 + 7;
    8. long hhash = 1;
    9. long B = 4;
    10. std::unordered_map<char, long> table{{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};
    11. for (int i = 0; i < 10; ++i) {
    12. //hhash = (hhash * B + s[i]) % ten9;
    13. hhash = (hhash * B + table[s[i]]) % ten9;
    14. }
    15. int P = 1;
    16. for (int i = 0; i < 9; ++i) {
    17. P = P * B % ten9;
    18. }
    19. std::unordered_map<long, int> hash2index; // hash值对应的最开始字符
    20. cout << hhash << endl;
    21. hash2index[hhash] = 0;
    22. std::vector result;
    23. for (int i = 10; i < s.size(); ++i) {
    24. long prev = (hhash - table[s[i-10]] * P) % ten9 * B;
    25. hhash = (prev + table[s[i]] * B % ten9) % ten9;
    26. cout << "i = " << i << " prev = " << prev << " new = " << hhash << endl;
    27. if (hash2index.find(hhash) != hash2index.end()) {
    28. result.push_back(s.substr(i-9, 10));
    29. } else {
    30. hash2index[hhash] = i - 9;
    31. }
    32. }
    33. return result;
    34. }
    35. };

  • 相关阅读:
    Jackson分析:侧向流动免疫分析在床旁检测中的优势
    【Fiddler】安卓7.0以上添加Fiddler/Charles证书到系统根证书(模拟器-雷电)
    alpine 设置时区,dockfile里面配置
    爬虫利器Frida RPC入门——夜神模拟器环境篇
    对象序列化运用
    2023高教社杯全国大学生数学建模竞赛C题代码解析
    论文初稿写到什么程度才算合格?
    Python内置库:shutil
    户外指南——时代产物
    java基于QuartzJobBean实现一个定时功能
  • 原文地址:https://blog.csdn.net/INGNIGHT/article/details/133624634