Starters 188-Swaps in a String · znzryb
## 思路讲解
我感觉可以搞dp(结果看了第二个样例觉得不太行)。
那么其实就是局部最优可以推得全局最优,非常经典的贪心。
那么为什么说这个B朝右朝左选一个最大的就最优了?不会相互影响吗?

那我们来看一个你认为会相互影响的,可以看到,这个是矛盾的。所以不会相互影响
```cpp
FOR(i,1,N){
++cnt[S[i]-'A'];
if(S[i]=='B'){
L[i]=cnt[0];
}else if(S[i]=='C'){
cnt[0]=0;
}else{
}
}
cnt.assign(10,0);
ROF(i,N,1){
++cnt[S[i]-'A'];
if(S[i]=='B'){
R[i]=cnt[2];
}else if(S[i]=='A'){
cnt[2]=0;
}else{
}
}
FOR(i,1,N){
if(S[i]=='B'){
ans+=max(L[i],R[i]);
}
}
```
## AC代码
[https://www.codechef.com/viewsolution/1167971455](https://www.codechef.com/viewsolution/1167971455)
::: 源代码
```cpp
// Problem: Swaps in a String
// Contest: CodeChef - START188
// URL: https://www.codechef.com/problems/SWAPSTR?tab=statement
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define lson(var) (var<<1)
#define rson(var) ((var<<1)+1)
using namespace std;
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e17)+9; // 1e18+9也是素数
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;
ll N,M,Q,X,K,lT;
char S[MAXN];
/*
*/
inline void init(){
}
inline void solve(){
cin>>N;
init();
FOR(i,1,N){
cin>>S[i];
}
vector<ll> L(N+5,0),R(N+5,0),cnt(10,0);
ll ans=0;
FOR(i,1,N){
++cnt[S[i]-'A'];
if(S[i]=='B'){
L[i]=cnt[0];
}else if(S[i]=='C'){
cnt[0]=0;
}else{
}
}
cnt.assign(10,0);
ROF(i,N,1){
++cnt[S[i]-'A'];
if(S[i]=='B'){
R[i]=cnt[2];
}else if(S[i]=='A'){
cnt[2]=0;
}else{
}
}
FOR(i,1,N){
if(S[i]=='B'){
ans+=max(L[i],R[i]);
}
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--){
solve();
}
// solve();
return 0;
}
/*
AC https://www.codechef.com/viewsolution/1167971455
*/
```
:::
## 心路历程(WA,TLE,MLE……)
写了一个下午,一直WA,思路错了。
::: 源代码
```cpp
// Problem: Swaps in a String
// Contest: CodeChef - START188
// URL: https://www.codechef.com/problems/SWAPSTR?tab=statement
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define lson(var) (var<<1)
#define rson(var) ((var<<1)+1)
using namespace std;
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e17)+9; // 1e18+9也是素数
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;
ll N,M,Q,X,K,lT;
char S[MAXN];
/*
*/
inline void init(){
}
inline void solve(){
cin>>N;
init();
FOR(i,1,N){
cin>>S[i];
}
vector<ll> cnt(10,0);
ll ans=0;
vector<bool> vis(N+5,false);
set<ll> s;vector<ll> wp;
// just process the part of string like "AABBCC" "ABC" "AABC"
// make these parts vis[i]=true;
FOR(i,1,N){
if(s.empty() && S[i]!='A'){
s.clear();wp.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
continue;
}
if(SZ(s)==1 && S[i]=='C'){
s.clear();wp.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
continue;
}
if(SZ(s)==2 && S[i]=='A') {
s.clear();wp.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
s.insert('A'),++cnt[S[i]-'A'];
continue;
}
if(SZ(s)==3 && S[i]!='C'){
s.clear();
ans+=max(cnt[0]*cnt[1],cnt[1]*cnt[2]);
cnt[0]=0;cnt[1]=0;cnt[2]=0;
if(S[i]=='A') s.insert('A'),++cnt[S[i]-'A'];
FOR(i,0,SZ(wp)-1){
vis[wp[i]]=true;
}
wp.clear();
continue;
}
s.insert(S[i]);
wp.pb(i);
++cnt[S[i]-'A'];
}
if(SZ(s)==3){
FOR(i,0,SZ(wp)-1){
vis[wp[i]]=true;
}
ans+=max(cnt[0]*cnt[1],cnt[1]*cnt[2]);
}
#ifdef LOCAL
FOR(i,1,N){cerr<<vis[i];}
cerr<<"\n";
debug(cnt[0]);debug(cnt[1]);debug(cnt[2]);
debug(ans);
#endif
cnt.assign(10,0);
s.clear();
// process part of string like "ABAB" "AB" "AABB"
ROF(i,N,1){
if(vis[i]){
s.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
continue;
}
if(S[i]=='C'){
s.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
continue;
}
if(S[i]=='A'){
ans+=cnt['B'-'A'];
}
++cnt[S[i]-'A'];
}
cnt.assign(10,0);
ROF(i,N,1){
if(vis[i]){
s.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
continue;
}
if(S[i]=='A'){
s.clear();
cnt[0]=0;cnt[1]=0;cnt[2]=0;
continue;
}
vis[i]=true;
if(S[i]=='B'){
ans+=cnt['C'-'A'];
}
++cnt[S[i]-'A'];
}
#ifdef LOCAL
debug(ans);
FOR(i,0,3){
cerr<<cnt[i]<<" ";
}
cerr<<"\n";
#endif
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--){
solve();
}
// solve();
return 0;
}
/*
hack
1
4
AABC
*/
```
:::