描述 Description | |||
基德穿破层层障碍终于来到了"忧郁的生日"面前."忧郁的生日"如前题所述封存于密码锁中.这个密码锁不是一般的密码锁,这是不一 般的密码锁!这个密码锁上显示着一串数字.旁边还有一串提示:The password is in the number.The number is on screen,you s- hould delete N numbers as 1,2 or 3...9.The last number should be the smallest of all.The last number is the passwo- rd(基德:靠,提示居然是E文,无视我400的智商吗?!).为了成功盗取并尽快赶去青子家,基德请你编一个程序解决这个密码锁。 | |||
输入格式 Input Format | |||
输入共2行 第一行为一个不超过250位的数字; 第二行则为N。 | |||
输出格式 Output Format | |||
输出只有一行,即PASSWORD. | |||
样例输入 Sample Input | |||
样例输出 Sample Output | |||
时间限制 Time Limitation | |||
1s | |||
注释 Hint | |||
数据范围 s不超过250位 n不超过s的长度 | |||
来源 Source | |||
经典改编题... From 玛维-影之歌 永远守望vijos... |
其实本题原意是贪心,向右一遍,再向左一遍,最后判断是否剩下的数都相等。
动态规划的话,方程为f[i,j] = min(f[i-1,k]×10+a[j] , f[i,j-1]),但是要用高精度,有点麻烦。
注意×10+a[j],高精度实现,是在开头加上a[j],而不是末尾,所以我错了很久。
但是由于j表示的是前j,有无j对后面无影响,所以可以降一维,不用枚举k,直接由f[i-1][j-1]转移过来就行。
#include <cstdio>
#include <cstring>
#include <string>
#define min(a,b) ((a)<(b)?(a):(b))
char _num[300];
char num[300];
long n;
long m=0;
unsigned char f[300][300][300];
inline void copy(unsigned char* a,unsigned char* b)
{
for (long i=0;i<a[0]+1;i++)
b[i] = a[i];
}
inline int compare(unsigned char* a,unsigned char* b)
{
if (a[0] < b[0])
return -1;
if (a[0] > b[0])
return 1;
for (long i=a[0];i>0;i--)
{
if (a[i] < b[i])
return -1;
if (a[i] > b[i])
return 1;
}
return 0;
}
unsigned char tmp[300];
inline void update(unsigned char* a,unsigned char* b,unsigned char c)
{
tmp[1] = c;
for (long i=1;i<b[0]+1;i++)
tmp[i+1] = b[i];
tmp[0] = b[0]+1;
if (compare(a,tmp)==1)
copy(tmp,a);
while (a[0]>0&&!a[a[0]]) a[0]--;
}
inline void update(unsigned char* a,unsigned char* b)
{
if (compare(a,b)==1)
copy(b,a);
while (a[0]>0&&!a[a[0]]) a[0]--;
}
void output(unsigned char* a)
{
if (a[0] == 0)
printf("0");
for (long i=a[0];i>0;i--)
{
printf("%ld",a[i]);
}
}
int main()
{
freopen("theft.in","r",stdin);
freopen("theft.out","w",stdout);
scanf("%s",num+1);
scanf("%ld",&n);
while (num[++m]) num[m] -= '0';
m--;
n = m-n;
for (long i=0;i<n+1;i++)
for (long j=0;j<m+1;j++)
{
f[i][j][0] = 250;
f[i][j][250] = 9;
}
for (long i=0;i<m+1;i++)
f[0][i][0] = f[0][i][1] = 0;
for (long i=1;i<n+1;i++)
{
for (long j=1;j<m+1;j++)
{
update(f[i][j],f[i][j-1]);
update(f[i][j],f[i-1][j-1],num[j]);
}
}
output(f[n][m]);
return 0;
}