一、題目
題面翻譯
給定長為 n n n 的僅由 R \texttt{R} R、 B \texttt{B} B、 ? \texttt{?} ? 組成的字符串 S S S,請你在 ? \texttt{?} ? 處填入 R \texttt{R} R 或 B \texttt{B} B,使得相鄰位置字符相同的數量最少。
譯者 @ajthreac
題目描述
As their story unravels, a timeless tale is told once again…
Shirahime, a friend of Mocha’s, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow.
There are $ n $ squares arranged in a row, and each of them can be painted either red or blue.
Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square.
Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color.
For example, the imperfectness of “BRRRBBR” is $ 3 $ , with “BB” occurred once and “RR” occurred twice.
Your goal is to minimize the imperfectness and print out the colors of the squares after painting.
輸入格式
Each test contains multiple test cases.
The first line contains a single integer $ t $ ( $ 1 \le t \le 100 $ ) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains an integer $ n $ ( $ 1\leq n\leq 100 $ ) — the length of the squares row.
The second line of each test case contains a string $ s $ with length $ n $ , containing characters ‘B’, ‘R’ and ‘?’. Here ‘B’ stands for a blue square, ‘R’ for a red square, and ‘?’ for a blank square.
輸出格式
For each test case, print a line with a string only containing ‘B’ and ‘R’, the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them.
樣例 #1
樣例輸入 #1
5
7
?R???BR
7
???R???
1
?
1
B
10
?R??RB??B?
樣例輸出 #1
BRRBRBR
BRBRBRB
B
B
BRRBRBBRBR
提示
In the first test case, if the squares are painted “BRRBRBR”, the imperfectness is $ 1 $ (since squares $ 2 $ and $ 3 $ have the same color), which is the minimum possible imperfectness.
二、分析
找到第一個不是’?'的字符,向兩邊修改;
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {int T;cin >> T;while(T--) {int n;cin >> n;string s;cin >> s;if(count(s.begin(),s.end(),'B')== 0 && count(s.begin(),s.end(),'R')==0){ for(int i = 0; i < s.size(); i++) {if(i % 2 == 0) s[i] = 'B';else s[i] = 'R';}cout<<s<<endl;continue;}for(int j=0;j<s.size();j++){if(s[j]!='?'){for(int k=j-1;k>=0;k--){if(s[k]!='?') break;if(s[k+1]=='B') s[k]='R';else s[k]='B';}for(int k=j+1;k<s.size();k++){if(s[k]!='?') break;if(s[k-1]=='B') s[k]='R';else s[k]='B';}}}cout<<s<<endl;}
}
例題2
假如是計算相鄰但不相等的對數,可以使用dp
樣例輸入 #1
5
7
?R???BR
7
???R???
1
?
1
B
10
?R??RB??B?
樣例輸出 #1
5
6
0
0
7
#include<iostream>
#include<cstring>
using namespace std;
const int N=200;
int dp[N][2]; //dp[i][0]表示第個位置選B的ans最大數量,dp[i][1]表示第i個位置選R的ans最大數量
int main()
{int T;cin>>T;while(T--){memset(dp,0,sizeof(dp));int n;string s;cin>>n>>s;s=" "+s;for(int i=2;i<=n;i++){if(s[i]=='?'||s[i]=='B'){dp[i][0]=max(dp[i-1][1]+1,dp[i-1][0]);}else{dp[i][0]=0;}if(s[i]=='?'||s[i]=='R'){dp[i][1]=max(dp[i-1][1]+1,dp[i-1][0]);}else{dp[i][1]=0;}}for(int i=1;i<=n;i++)cout<<dp[i][1]<<" ";cout<<endl;cout<<max(dp[n][0],dp[n][1])<<endl;}
}