PAT A1112

思路:每读入字符判断是否为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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值