Niuke Practice 71——Palindrome Number (Simulation + Details)
Really details.
Ideas:
Record the number of each digit as num[i]. According to the meaning of the question, we can know:
The situations that cannot be constructed are:
1. At least two num[i] are odd numbers.
2. num[0]>=2 and there is only one other digit and only appears once, such as 050, leading 0 cannot be avoided
Pay attention to the last example.
The normal practice is divided into two categories. If num[0]==0, it means that there is no need to consider the leading zero problem. Just enumerate half of the first half from small to large, and flip to get the second half; otherwise, you must first Put the smallest non-zero bit at the beginning, and then repeat the previous steps. If num[i]%2, put the extra one in the middle.
Attach the garbage code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
char F[200];
inline void out(I_int x)
{
if (x == 0)
return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0)
putchar('-');
int cnt = 0;
while (tmp > 0)
{
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0)
putchar(F[--cnt]);
//cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
ll res=1;
while(b)
{
if(b&1)
res=res*a%p;
a=a*a%p;
b>>=1;
}
return res;
}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int N=410,mod=1e8;
const double PI = 3.1415926535;
const double eps=1e-6;
const int maxn=1100;
int main()
{
int num[10];
int cnt=0,sum=0,pos;
for(int i=0; i<=9; i++)
{
num[i]=read();
if(num[i]&1)
cnt++,pos=i;
if(num[i]) sum++;
}
if(cnt>1||(num[0]>=2&&sum==2&&num[pos]==1))
puts("-1");
else if(sum==1&&num[0]==1) puts("0");
else
{
if(num[0]==0)
{
string s;
int flag=-1;
for(int i=0; i<=9; i++)
if(num[i])
{
for(int j=1; j<=num[i]/2; j++)
{
char t=i+'0';
s=s+t;
}
if(num[i]&1)
flag=i;
}
cout<<s;
if(flag!=-1)
cout<<flag;
reverse(s.begin(),s.end());
cout<<s<<endl;
}
else
{
string s;
int pos=1;
while(!num[pos]) pos++;
char ch=pos+'0';
s+=ch;
num[pos]-=2;
int flag=-1;
for(int i=0; i<=9; i++)
if(num[i])
{
for(int j=1; j<=num[i]/2; j++)
{
char t=i+'0';
s=s+t;
}
if(num[i]&1)
flag=i;
}
cout<<s;
if(flag!=-1)
cout<<flag;
reverse(s.begin(),s.end());
cout<<s<<endl;
}
}
return 0;
}