字节8.9笔试第一题讨论
字节8.9笔试第一题
第一题我的思路是用双指针表示一个区间,然后把区间内的字符放到multiset内,区间大小就是数量最多的字符的个数+m. 自己测试的十几个数据都过了,为啥提交以后是0%? 有没有大佬帮忙看一下。 #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int m, n; string str; char* _begin; char* _end; multiset<char> ms; char maxchar; int maxcharnum = 0; long long maxnum = 0; cin >> n >> m; cin >> str; if(n == 1 || n - m == 1) { cout << n; return 0; } _begin = &str[0]; _end = &str[0]; cout << *_begin <<endl; cout << str.size() << " " << str[str.size()] << endl; while (_end != &str[str.size()]) { ms.insert(*_end); if(ms.count(*_end) > maxcharnum) //选择数量最多的元素 { maxcharnum = ms.count(*_end); maxchar = *_end; } _end++; if(maxcharnum + m < (_end - _begin)) { ms.erase(ms.find(*_begin)); _begin++; if(maxchar == *_begin) maxcharnum--; } else { maxnum = max(maxnum, (_end - _begin)); } } cout << maxnum; return 0; }