Divisibility by Thirty-six(字符串维护+整除问题+贪心)

Description

  There is a string containing only decimal digit characters. The length of the string is between 1 and 1,000. Using characters of the string, you have to construct the maximum number which divides by thirty-six without remainder. Each character of the string may not be used more than once.

Input

  The first line of the input contains an integer T (T <= 1000), indicating the number of cases. Each case begins with a line containing a single line representing the source string.

Output

  For each test case, print a line containing the test case number (beginning with 1) and the decimal representation of the maximum number (leading zeroes should be omitted). If no number can be constructed, please output “impossible” instead. 

Sample Input

2
03061
345

Sample Output

Case 1: 6300
Case 2: impossible

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005
#define M 15
int dir[30][2]={{0, 0},{4, 0},{8, 0},{1, 2},{1, 6},{2, 0},{2, 4},{2, 8},{3, 2},{3, 6},{4, 4},{8, 4},{5, 2},{5, 6},{6, 0},{6, 4},{6, 8},{7, 2},{7, 6},{8, 8},{9, 2},{9, 6}};
int s[N],temp,rec[M],cnt[M],flag,id,len;
char ans[N];
void put(int n[],int k,char *str)
{
    int l=0,i,j;
    for(i=9;i>=0;i--)
    {
        for(j=0;j<n[i];j++)
        {
            str[l]=i+'0';
            l+=1;
        }
    }
    for(i=0;i<2;i++)
    {
        str[l]=dir[k][i]+'0';
        l+=1;
    }
    str[l]='\0';
}
int compare(char *str,char *s)
{
    int i,j;
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]!=s[i]) return str[i]<s[i];
    }
    return 0;
}
void judge(int n)
{
    char now[N];
    put(cnt,id,now);
    if(n<len||(compare(ans,now)&&n==len))
    {
        flag=1;
        len=n;
        strcpy(ans,now);
    }
}
void dfs(int n,int d,int sum)
{
    if(d>=n)
    {
        if((sum-temp)%9==0) judge(n);
            return ;
    }
    for(int i=1;i<=8;i++)
    {
        if(!cnt[i]) continue;
        cnt[i]-=1;
        dfs(n,d+1,sum+i);
        cnt[i]+=1;
    }
}
void Search(int d)
{
    for(int i=0;i<2;i++)
    {
        cnt[dir[d][i]]-=1;
        if(cnt[dir[d][i]]<0) return ;
    }
    id=d;
    for(int i=0;i<=temp&&i<=len;i++)
    {
        dfs(i,0,0);
    }
}
int isok(char *str)
{
    int sum=0,l=strlen(str);
    if(l==0&&rec[0]==0) return 0;
    for(int i=0;i<l;i++)
    {
        sum+=str[i]-'0';
    }
    return !sum;
}
void solve()
{
    int i;
    for(i=0;i<22;i++)
    {
        memcpy(cnt,rec,sizeof(rec));
        Search(i);
    }
}
int main()
{
   int T,kase=1;
   scanf("%d",&T);
   while(T--)
   {
      char num[N];
      scanf("%s",num);
      len=strlen(num);
      temp=flag=0;
      memset(rec,0,sizeof(rec));
      memset(ans,0,sizeof(ans));
      for(int i=0;i<len;i++)
      {
          int item=num[i]-'0';
          temp+=item;
          rec[item]+=1;
      }
      solve();
      if(isok(ans))
         printf("Case %d: 0\n",kase);
      else if(flag)
         printf("Case %d: %s\n",kase,ans);
      else
         printf("Case %d: impossible\n",kase);
      kase+=1;
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCMU_2024/article/details/81590121