1. 程式人生 > 實用技巧 >CF 1379 A. Acacius and String

CF 1379 A. Acacius and String

傳送門

題目:可以改變'?'為任意'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\n", (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 }