C++ Basic Prefix and Syntax Problems (November 23, 2025)

Problem Description: The Three-Color Wall of Little Code BrotherC++ Basic Prefix and Syntax Problems (November 23, 2025)Verify whether the prefix sum array construction is successfulC++ Basic Prefix and Syntax Problems (November 23, 2025)The code is as follows:

#include <bits/stdc++.h> using namespace std;const int N = 1007;int n,m;string s;int w[N] = {0},b[N] = {0},r[N] = {0};
int money(char c){int ans = 0;//WRWRWfor(int i=0;i<s.size();i++){if(s[i]!=c){ ans++; } }return ans;}
int main(){ cin >> n >> m;for(int i=1;i<=n;i++){ cin >> s;//WRWRW//BWRWB//WRWRW//RWBWR//w[1] 2 //w[2] 5 //w[3] 7//w[4] 10 w[i] = w[i-1] + money('W');//b[1] 5//b[2] 8//b[3] 13//b[4] 17 b[i] = b[i-1] + money('B');//r[1] 3//r[2] 7//r[3] 10//r[4] 13 r[i] = r[i-1] + money('R'); }
for(int i=1;i<=n;i++){ cout << w[i] << " "; } cout << endl;for(int i=1;i<=n;i++){ cout << b[i] << " "; } cout << endl;for(int i=1;i<=n;i++){ cout << r[i] << " "; }return 0;}

The final effect is as follows:C++ Basic Prefix and Syntax Problems (November 23, 2025)The code is as follows:

#include <bits/stdc++.h> using namespace std;const int N = 1007;int n,m;string s;int w[N] = {0},b[N] = {0},r[N] = {0};
int money(char c){int ans = 0;//WRWRWfor(int i=0;i<s.size();i++){if(s[i]!=c){ ans++; } }return ans;}
int main(){ cin >> n >> m;for(int i=1;i<=n;i++){ cin >> s;//WRWRW//BWRWB//WRWRW//RWBWR//w[1] 2 //w[2] 5 //w[3] 7//w[4] 10 w[i] = w[i-1] + money('W');//b[1] 5//b[2] 8//b[3] 13//b[4] 17 b[i] = b[i-1] + money('B');//r[1] 3//r[2] 7//r[3] 10//r[4] 13 r[i] = r[i-1] + money('R'); }
// for(int i=1;i<=n;i++){// cout << w[i] << " ";// }// cout << endl;// for(int i=1;i<=n;i++){// cout << b[i] << " ";// }// cout << endl;// for(int i=1;i<=n;i++){// cout << r[i] << " ";// }int ans = 0x3f3f3f3f;for(int i=1;i<n-1;i++){for(int j=i+1;j<n;j++){int temp = w[i] + b[j] - b[i] + r[n] - r[j]; ans = min(ans,temp); } } cout << ans;return 0;}

Leave a Comment