题目:可以改变'?'为任意'a'~'z'的字符,可不可以让s有且仅有一个子串为"abacaba"。
思路:暴力就行,枚举每个位置开始7个字符能否组成"abacaba",可以的话在判断此时把这7个位置的字符变成"abacaba"时,s有几个"abacaba"子串。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <string> 6 #include <vector> 7 #include <cmath> 8 9 using namespace std; 10 11 #define ll long long 12 #define pb push_back 13 #define fi first 14 #define se second 15 16 const int N = 2e5 + 10; 17 char s[N]; 18 int len; 19 20 void solve() 21 { 22 string p = "abacaba"; 23 int T; 24 cin >> T; 25 while(T--){ 26 string s, tmp; 27 int n; 28 cin >> n >> s; 29 30 int good = 0; 31 for(int i = 0; i < n; ++i){ 32 if(i + 6 >= n) break; 33 34 //几个字符匹配 35 int ok = 0; 36 for(int j = 0; j < 7; ++j){ 37 if(s[i + j] == '?' || p[j] == s[i + j]) ok++; 38 else break; 39 } 40 if(ok == 7){ 41 tmp = s; 42 for(int j = 0; j < 7; ++j) tmp[i + j] = p[j]; 43 44 //出现几次 45 int cnt = 0; 46 string::size_type inx = 0; 47 while(1){ 48 inx = tmp.find(p, inx); 49 //printf("inx = %d ", (int)inx); 50 if(inx == tmp.npos) break; 51 inx = inx + 3; 52 cnt++; 53 } 54 if(cnt == 1){ 55 good = 1; 56 cout << "yes" << endl; 57 for(int j = 0; j < n; ++j){ 58 cout << (tmp[j] == '?' ? 'z' : tmp[j]); 59 } 60 cout << endl; 61 break; 62 } 63 } 64 } 65 66 if(!good) cout << "no" << endl; 67 } 68 } 69 70 int main() 71 { 72 ios::sync_with_stdio(false); 73 cin.tie(0); 74 cout.tie(0); 75 solve(); 76 77 return 0; 78 }