一定要用gcc调试。。。贪图vs的编辑方便,在本机上都过的case就是提交不能过。。。 换了gcc才发现问题。
static const char digit2char[4] = { 'A', 'C', 'G', 'T' };//A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T
static const int char2digit[26] = {0,0,1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,3};int32_t encodeDNA(const string& s, int begin)
{int32_t r = 0;for (int i = begin; i < 10; i++){r = r | (char2digit[s[i] - 'A'] << (2 * (9 - i)));}return r;
}string decodeDNA(int32_t val)
{char res[11] = { 0 };for (int i = 0; i < 10; i++){res[i] = digit2char[(val >> (2 * (9 - i))) & 3];}return string(res);
}int32_t shiftRightByOne(int32_t val, char c)
{int32_t res = ((val & 0x3ffff) << 2) |(char2digit[c-'A']);return res;
}
vector<string> findRepeatedDnaSequences(string s) {vector<string> res;if (s.length() <= 10){return res;}unordered_map<int32_t, size_t> checkMap;set<int32_t> tmpRes;int32_t encodeVal = encodeDNA(s, 0);checkMap.insert(make_pair(encodeVal, 9));for (int i = 10; i < s.length(); i++){encodeVal = shiftRightByOne(encodeVal,s[i]);auto it = checkMap.find(encodeVal);if (it == checkMap.end()){checkMap.insert(make_pair(encodeVal,i));}else{tmpRes.insert(encodeVal);}}for (auto& i : tmpRes){res.push_back(decodeDNA(i));}return res;
}