思路:每读入字符判断是否为stucked,若不是加入not_stuck表,若是加入stuck表,最后输出stuck表及按规则输出原串
坑点:要分两次遍历,第一次更新not_stuck表,第二次更新stuck表,否则遇到一个字符未被阻塞还需去检查之前有没有加入stuck表
AC代码如下
#include <iostream>
#include <cstring>
using namespace std;
const int max_n = 1010;
char not_stuck[40]={'\0'};
char stuck[40]={'\0'};
int s_count=0,ns_count=0;
bool in_not_stuck(char a){
for(int i = 0 ;i < ns_count ;i++)
if(a == not_stuck[i])
return true;
return false;
}
bool in_stuck(char a){
for(int i = 0 ;i < s_count ;i++)
if(a == stuck[i])
return true;
return false;
}
bool is_stucked(char *pa,int k){
if(in_not_stuck(*pa)) return false;
for(int i = 0 ;i < k;i++){
if(*pa != *(pa+i)) return false;
}
return true;
}
int main(){
int k;
char str[max_n];
cin>>k>>str;
int len = strlen(str);
for(int i = 0 ;i <len ;i++){
if(!is_stucked(str+i,k)){
if(!in_not_stuck(str[i]))
not_stuck[ns_count++] = str[i];
}
else
i += (k-1);
}
for(int i = 0 ;i <len ;i++){
if(is_stucked(str+i,k)){
if(!in_stuck(str[i]))
stuck[s_count++] = str[i];
i += (k-1);
}
}
cout<<stuck<<endl;
for(int i = 0 ;i <len ;i++){
cout<<str[i];
if(in_stuck(str[i]))
i += (k-1);
}
return 0;
}