Solution from tester.
Analysis
Consider an upper bound of .
When a majority of doesn’t exist
Since a string of length must be a palindrome, must be no more than . The following algorithm gives such a construction.
Define a reduction is to choose any mode of the and any other existing character(s) and make them as a subsequence, then delete them from the string. If is even, do reduction times. If is odd, do reduction once and then do reduction times.
One can inductively proves that the algorithm is correct by the fact that after each step, a majority of still doesn’t exist. One can implement the algorithm fast enough by maintaining characters by buckets.
When a majority of exists
Let the indices of non-majority characters be . Since any string consisting with characters all the same must be a palindrome, must be no more than .
When
, which means there is no solution.
When
, which means the only solution is to choose the whole string as a substring, if possible. Just check whether .
When
The idea is the following: for most non-majority characters, pair them with one majority character each, and add the rest majority characters to some specific subsequences.
When
For every , choose any majority character left to pair with. Add the rest majority characters to the subsequence containing . Since , the first character of the subsequence is (non-majority), while the last is majority, therefore the construction is valid.
When
For every , choose any majority character left to pair with. Add the rest majority characters to the subsequence containing . Since , the last character of the subsequence is (non-majority), while the first is majority, therefore the construction is valid.
When
Let pair with , pair with , and for , choose any majority character left to pair with. For each majority character left, add it to the subsequence containing , if , otherwise . Since implies , the last character of the subsequence containing is (non-majority), while the first is majority, and the first character of the subsequence containing is (non-majority), while the last is majority, therefore the construction is valid.
Code
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,del##i##verme=int(n);i<del##i##verme;++i)
#define rep1(i,n) for(int i=1,parano##i##a=int(n);i<=parano##i##a;++i)
#define per(i,n) for(int i=int(n)-1;i>=0;--i)
#define per1(i,n) for(int i=int(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define y0 LingLuo
#define y1 VividCycle
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
unsigned time_related_rand()
{
return unsigned(std::chrono::steady_clock::now().time_since_epoch().count());
}
int c,t,n;
string s;
vector<int> idx[28];
vector<int> p,q;
int mod;
vector<int> ans[50004];int cnt;
void report()
{
cout<<"Huoyu\n";
cout<<cnt<<"\n";
rep1(i,cnt)
{
cout<<ans[i].size();
sort(ans[i].begin(),ans[i].end());
for(int x:ans[i])cout<<" "<<x;
cout<<'\n';
}
return ;
}
void Q()
{
cin>>n>>s;
rep1(i,26) idx[i].clear();
rep(i,n) idx[s[i]&31].pb(i+1);
mod=0;rep1(i,26) if(int(idx[i].size())*2>n) mod=i;
rep1(i,n/2) ans[i].clear();
cnt=0;
if(mod==0)
{
if(n&1)
{
rep1(_,n/2)
{
int cur=1;rep1(i,26) if(idx[i].size()>idx[cur].size()) cur=i;
int nxt=1;rep1(i,26) if(i!=cur&&!idx[i].empty()){nxt=i;break;}
++cnt;
if(_==1)
{
int lst=1;rep1(i,26) if(i!=nxt&&i!=cur&&!idx[i].empty()){lst=i;break;}
ans[cnt].pb(idx[lst].back());idx[lst].pop_back();
}
ans[cnt].pb(idx[cur].back());idx[cur].pop_back();
ans[cnt].pb(idx[nxt].back());idx[nxt].pop_back();
}
}
else
{
rep1(_,n/2)
{
int cur=1;rep1(i,26) if(idx[i].size()>idx[cur].size()) cur=i;
int nxt=1;rep1(i,26) if(i!=cur&&!idx[i].empty()){nxt=i;break;}
++cnt;
ans[cnt].pb(idx[cur].back());idx[cur].pop_back();
ans[cnt].pb(idx[nxt].back());idx[nxt].pop_back();
}
}
report();
return ;
}
p.clear();q.clear();
rep1(i,26) idx[i].clear();
rep(i,n) if((s[i]&31)==mod) p.pb(i+1); else q.pb(i+1);
if(q.empty())
{
cout<<"Shuiniao\n";return ;
}
if(int(q.size())==1)
{
if(q[0]*2-1==n)
{
cout<<"Shuiniao\n";return ;
}
cnt=1;
rep1(i,n) ans[1].pb(i);
report();
return ;
}
if(q[0]==1)
{
for(int x:q) if(x!=1)
{
ans[++cnt].pb(x);
ans[cnt].pb(p.back());p.pop_back();
}
ans[++cnt].pb(1);
for(int x:p) ans[cnt].pb(x);
report();
return ;
}
if(q.back()==n)
{
for(int x:q) if(x!=n)
{
ans[++cnt].pb(x);
ans[cnt].pb(p.back());p.pop_back();
}
ans[++cnt].pb(n);
for(int x:p) ans[cnt].pb(x);
report();
return ;
}
int a=q.back();q.pop_back();
int b=q.back();q.pop_back();
ans[++cnt].pb(a);
ans[++cnt].pb(b);ans[cnt].pb(n);
p.pop_back();
for(int x:q)
{
ans[++cnt].pb(x);ans[cnt].pb(p.back());p.pop_back();
}
for(int x:p)if(x<a) ans[1].pb(x); else ans[2].pb(x);
report();
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>c>>t;while(t--)Q();return 0;
}