分值 : 20 难度 : 简单题 思路 : 思路很简单,但是有些细节一开始没有想到位,导致有测试点翻车 坑点 : 比如k=3,一个字母连续出现了4 次,那他照样不是,判断条件为每次连续的次数是否%k=0
123456789101112131415161718192021222324252627282930313233343536373839404142
#include <iostream>using namespace std ;int flag[259];int main() { int k; string s; cin >> k >> s; char old = ' '; int count = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == old) { count++; if (count%k ==0 && flag[(int) s[i]] != -1) flag[(int) s[i]] = 1; } else { if ( count%k ==0 && flag[(int) s[i]] != -1) flag[(int) old] = 1; else if ( count%k !=0 ) flag[(int) old] = -1; count = 1; old = s[i]; } } for (int i = 0; i < s.size(); i++) if (flag[(int) s[i]] == 1) { cout << s[i]; flag[(int) s[i]] = 2; } cout << endl; for (int i = 0; i < s.size();) { //cout << s[i] <<" " <<flag[(int)s[i]]<<endl ; if (flag[(int) s[i]] != 2) { cout << s[i]; i++; } else { cout << s[i]; i += k; } }}