2019牛客暑期多校训练营(第七场)A(暴力)

参考博客: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值