START195B——String Deletions(注意前导一致和后导一致)(01字符串) · znzryb
## 思路讲解
可以看提交链接
## AC代码
[https://www.codechef.com/viewsolution/1173432221](https://www.codechef.com/viewsolution/1173432221)
::: 源代码
```cpp
// Problem: String Deletions
// Contest: CodeChef - START195B
// URL: https://www.codechef.com/START195B/problems/STRDEL7
// 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 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)
#define V vector
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 vll=vector<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 MAXM=(ll)1e6+10;constexpr ll MAXV=(ll)1e6+10;
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
/*
*/
char S[MAXN];
inline void __(){
cin>>N;
FOR(i,1,N){
cin>>S[i];
// ++cnt[S[i]-'0'];
}
vector<vector<ll> > g(4);
vll wp;
ll dec=0;
FOR(i,1,N){
if(g[S[i]-'0'].empty() || i-g[S[i]-'0'].back()>1){
wp.pb(i);
}else{
++dec;
// #ifdef LOCAL
// debug(i);
// debug(i-g[S[i]-'0'].back());
// debug(S[i]);
// debug(g[S[i]-'0'].back());
// #endif
}
g[S[i]-'0'].pb(i);
}
if(SZ(g[0])==N || SZ(g[1])==N){
cout<<N<<"\n";
return;
}
ll ans=N-dec;
if(SZ(wp)>=3){
vll cnt(4,0);
ll lans=0;
if(S[wp.back()]!=S[wp.front()]){
lans=SZ(wp)-3;
}else{
lans=SZ(wp)-2;
}
ans-=lans;
}
ROF(i,N,1){
if(S[i]==S[N]){
++ans;
}else{
break;
}
}
FOR(i,1,N){
if(S[i]==S[1]){
++ans;
}else{
break;
}
}
ans-=1;
ans-=1;
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--)
__();
return 0;
}
/*
AC
https://www.codechef.com/viewsolution/1173432221
1
5
11010
4
1
5
01010
ans:2
*/
```
:::
## 心路历程(WA,TLE,MLE……)