A. Acacius and String(暴力&枚举)

A. Acacius and String(暴力&枚举)

前话:本来想打下这场的,看了一眼 A A 题,发现我自己构造的数据过不了,一时没想到什么好的方法,卡了10分钟,就没打。

思路:暴力枚举。

因为数据实在太小,所以可以暴力枚举目标字符串 T = a b a c a b a T=abacaba 在原串那个位置。

另外需要注意的是:当枚举某一个位置成为答案时,还需要保证该串在其他位置没有

字符串,这里需要特判一下。

e p : n = 18 , s = a b a c a ? a c a b a a b a ? a b a ep:n=18,s=abaca?acaba|aba?aba

这里就不能直接用前一个 ? ? ,如果将其该为 b b 就会变成两个。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a) memset(a,0,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(false),cout.tie(0)
const string T="abacaba";
string s;
int n;
bool check(string &a){
	int cnt=0;
	for(int i=0;i+7<=n;i++)
		if(a.substr(i,7)==T) cnt++;
	return cnt==1;
}
int main(){
	IOS;
	int t;
	cin>>t;
	while(t--){
		cin>>n>>s;
		int f=0;
		for(int i=0;i+7<=n;i++){
			string ss=s;
			bool ok=1;
			for(int j=0;j<7;j++){
				if(ss[i+j]!=T[j]&&ss[i+j]!='?'){
					ok=0;
					break;
				}
				ss[i+j]=T[j];	
			}
			if(ok&&check(ss)){
				for(int j=0;j<n;j++){
					if(ss[j]=='?') ss[j]='h';
				}
				cout<<"Yes\n";
				f=1;
				cout<<ss<<endl;
				break;
			}
		}
		if(!f) cout<<"No\n";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/107452437
今日推荐