题目:Jam的计数法(水题)

题目

传送门

思路

除了暴力我真没有想出其他的思路。。。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int b,t,w;
char a[105];
char s_s,s_t;
bool s[105];
bool pd()
{
	int tot=0;
	for(int i=s_t;i>=s_s;i--)
	{
		if(s[i])
			tot++;
		else
			break;
	}
	if(tot==w)
		return 1;
	return 0;
}
void add()
{
	int tot=0;
	for(int i=s_t;i>=s_s;i--)
	{
		if(s[i]==1)
		{
			s[i]=0;
			tot++;
		}
		else
		{
			for(int j=i;j>=s_s;j--)
			{
				if(s[j]==1)
				{
					tot++;
					s[j]=0;
					for(int k=j+1;tot;k++,tot--)
					{
						s[k]=1;
					}
					return;
				}
				
			}
		}
	}
}
void pr()
{
	for(int i=s_s;i<=s_t;i++)
	{
		if(s[i])
		{
			cout<<(char)(i);
		}
	}
	cout<<'\n';
}
int main()
{
	//freopen("number.in","r",stdin);
	//freopen("number.out","w",stdout);
	cin>>b>>t>>w;
	cin>>a;
	s_s=b+'a'-1;
	s_t=t+'a'-1;
	int lena=strlen(a);
	for(int i=0;i<lena;i++)
		s[a[i]]=1;
	if(pd())
		return 0;
	for(int i=1;i<=5;i++)
	{
		add();
		pr();
		if(pd())
			break;
	}
	return 0;
}
posted @ 2019-11-12 21:25  loney_s  阅读(171)  评论(0)    收藏  举报