题意: 是否能将原字符串中的所以 ‘?’ 替换成某些小写字母,使得 “abacaba” 在结果字符串只出现一次。
思路:
- 题目数据比较小,可以直接暴力枚举目标字符串"abacaba"在原串的位置。
- 需注意当枚举某一个位置匹配到"abacaba"时,还得保证其他位置不会出现该字符串,因此需要特判一下。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
int t, n;
string s, S = "abacaba";
bool check(string &x){
int cnt = 0;
for(int i = 0; i+7 <= n; i ++)
if(x.substr(i,7) == S) cnt ++;
return cnt == 1;
}
signed main()
{
IOS;
cin >> t;
while(t --){
cin >> n >> s;
int yes = 0;
for(int i = 0; i+7 <= n; i ++){
string tmp = s;
int ok = 1;
for(int j = 0; j < 7; j ++){
if(tmp[i+j] != S[j] && tmp[i+j] != '?'){
ok = 0;
break;
}
tmp[i+j] = S[j];
}
if(ok && check(tmp)){
for(int j = 0; j < n; j ++){
if(tmp[j] == '?') tmp[j] = 'h';
}
cout << "Yes" << endl;
yes = 1;
cout << tmp << endl;
break;
}
}
if(!yes) cout <<"No" << endl;
}
return 0;
}