題目
題目連接:https://codeforc.es/contest/1392/problem/Dios
思路
分兩種狀況
一.只有L/R
n/3向上取整
二.都有
每次取連續段的L/R 每次加上連續段長度/3便可
spa
代碼
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<cctype> #include<ctime> #include<iostream> #include<string> #include<map> #include<queue> #include<stack> #include<set> #include<vector> #include<iomanip> #include<list> #include<bitset> #include<sstream> #include<fstream> #include<complex> #include<algorithm> #if __cplusplus >= 201103L #include <unordered_map> #include <unordered_set> #endif #define ll long long using namespace std; const int INF = 0x3f3f3f3f; int dp[100010][2]; int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int t; cin>>t; while(t--){ int n; cin>>n; string s; cin>>s; int cnt=0; for(int i=n-1;i>=0;i--){ if(s[i]==s[0]){ cnt++; } else break; } // cout<<cnt<<endl; vector<int> v; int cnt1=0; for(int i=0;i<n-cnt;i++){ if(s[i]!=s[i+1]){ cnt1++; v.push_back(cnt1); cnt1=0; } else cnt1++; } //for(int i=0;i<v.size();i++) cout<<v[i]<<" \n"[i==v.size()-1]; if(cnt==n){ cout<<n/3+(n%3==0?0:1)<<endl; } else{ int ans=0; for(int i=0;i<v.size();i++){ if(i==0){ ans+=(v[i]+cnt)/3; } else ans+=v[i]/3; } cout<<ans<<endl; } } return 0; }