给定一个序列,只能拿走D,隔壁的会翻转,问能否全部拿走。
注意到如果能拿走的话,拿D的顺序是没关系的。模拟即可
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> const int maxn = 1e5+20; char str[maxn]; char sub[maxn]; void work () { char book[256]; book['B']='D'; book['D']='B'; int n; scanf("%d",&n); scanf("%s",str+1); strcpy(sub+1,str+1); int did=0; int cnt=0; vector<int>ans; int begin=1; int flag=0; for (int i=1;i<=n;++i) { if (str[i]=='D') { flag=1; cnt++; did += cnt; cnt=0; str[i+1]=book[str[i+1]]; for (int j=i;j>=begin;--j) { ans.push_back(j); } begin=i+1; } else cnt++; } if (did==n) { printf ("Y "); for (int i=0;i<ans.size();++i) { printf ("%d ",ans[i]); } printf (" "); } else printf ("N "); return ; } int main() { #ifdef local freopen("data.txt","r",stdin); #endif int t; scanf ("%d",&t); while(t--) work(); return 0; }