参考博客:https://www.cnblogs.com/JHSeng/p/11322901.html
#include<queue>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
vector<string> ans;
bool check(string s) //判断一个串是否为完美串
{
string str;
str=s+s;
int len=s.length();
for(int i=1;i<len;++i) //枚举起点
{
string s0;
for(int j=0;j<len;++j)
s0+=str[i+j];
if(s0<s) return 0; //所得串<原串,就不是完美串了
}
return 1;
}
int main()
{
int T;
cin>>T;
while(T--)
{
string s;
cin>>s;
string fro,back,mid; //最前面的1串和最后面的0串都可以先砍掉,mid记录的是中间那段串
int l,r,len;
len=s.length();
for(l=0;l<len;++l)
if(s[l]=='1') fro+='1';
else break;
for(r=len-1;r>=l;--r)
if(s[r]=='0') back+='0';
else break;
if(!fro.empty()) ans.push_back(fro); //发现用vector很好写(参考上述博客的)
for(int i=l;i<=r;i++)
mid+=s[i];
len=mid.length();
l=0;
while(l<len)
{
for(int i=len-1;i>=l;--i) //枚举终点,枚举[l,i]之间的所有串
{
string temp;
for(int j=l;j<=i;++j)
temp+=mid[j];
if(check(temp))
{
if(!temp.empty()) ans.push_back(temp); //注意,temp有可能为空串,空串不能入vector
l=i+1;
}
}
}
if(!back.empty()) ans.push_back(back);
for(int i=0;i<ans.size();++i)
{
cout<<ans[i];
if(i==ans.size()-1) cout<<"\n";
else cout<<" ";
}
ans.clear();
}
return 0;
}