文章目录 A. Make it White B. Following the String C.Choose the Different Ones! D. Find the Different Ones!
A. Make it White
#include<bits/stdc++.h>using namespace std;void solve()
{int n;cin >> n;string s; cin >> s;int flag = 0;int x = 0, y = -1;for (int i = 0; i < n; i++){if (flag == 0 && s[i] == 'B'){flag = 1;x = i;}if (s[i] == 'B')y = i;}cout << y - x + 1 << endl;
}int main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--)solve();return 0;
}
B. Following the String
#include<bits/stdc++.h>using namespace std;void solve()
{int n;cin >> n;int a[n + 1];for(int i = 1; i <= n; i ++ ) cin >> a[i];int m[26] = {0};for(int i = 1; i <= n; i ++ ){for(int j = 0; j < 26; j ++ ){if(m[j] == a[i]){m[j] ++ ;char c = j + 'a';cout << c;break;}}}cout << endl;
}int main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--) solve();return 0;
}
C.Choose the Different Ones!
#include<bits/stdc++.h>using namespace std;void solve()
{int n1, n2, k;cin >> n1 >> n2 >> k;int a1[n1], a2[n2];int b[k+1] = {0};for(int i = 0; i < n1; i ++ ){cin >> a1[i];if(a1[i] <= k) b[a1[i]] = 1;}for(int i = 0; i < n2; i ++ ){cin >> a2[i];if(a2[i] <= k && b[a2[i]] == 1) b[a2[i]] = 3;else if(a2[i] <= k && b[a2[i]] != 1 && b[a2[i]] != 3) b[a2[i]] = 2;}int s1 = 0, s2 = 0, s3 = 0;for(int i = 1; i <= k; i ++ ){if(b[i] == 1) s1 ++ ;else if(b[i] == 2) s2 ++ ;else if(b[i] == 3) s3 ++ ;}if(s1 + s2 + s3 != k) cout << "NO" <<endl;else{if(s1 <= k/2 && s1 + s3 >= k/2) cout << "YES" << endl;else cout << "NO" << endl;}
}int main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--) solve();return 0;
}
D. Find the Different Ones!
#include<bits/stdc++.h>using namespace std;void solve()
{int n; cin >> n; vector<int> a(n + 1), s(n + 1), nxt(n + 1, -1);for(int i = 1; i <= n; i ++) cin >> a[i];int i = 1;while(i <= n){int j = i;while(j + 1 <= n && a[i] == a[j + 1]) j ++;for(int k = i; k <= j; k ++) nxt[k] = j + 1;i = j + 1;}int q; cin >> q;while(q --){int l, r; cin >> l >> r;if(nxt[l] <= r) cout << l << " " << nxt[l] << "\n";else cout << "-1 -1\n";}cout << "\n";
}signed main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--) solve();return 0;
}