cf 496B Secret Combination

时间:2024-08-30 18:34:26

题目链接:B. Secret Combination

You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.

You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.

题意:给出一个n位数和两种操作,操作1:给这个数的每个位上的数值加1(9会变成0),操作2:n位数右移一位(最后一位也就是个位上的数移到最高位)。现在要求运用这两种操作来使这个数最小化。

解法:因题目要求最小化这个数,对于操作1而言,要么让高位开始变为0(比如998923),要么让低位开始变为0(比如23789899),然后通过右移,此时才能使当前数更小化。所以我们可以先保存这个n位数所有右移后的数,然后对每个数保证最高位为0的情况下进行对这个数最小化,然后更新答案即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long ll;
const int maxn=+;
int n;
char str[maxn][maxn];
char S[maxn];
int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(S,,sizeof(S));
scanf("%s",str[]);
int len=strlen(str[]);
for (int i= ;i<len ;i++) S[i]='';
char s2[maxn];
memset(s2,,sizeof(s2)); strcpy(s2,str[]);
int num=-(s2[]-'');
for (int j= ;j<len ;j++)
{
int k=(s2[j]-''+num)%;
s2[j]=k+'';
}
if (strcmp(S,s2)>) strcpy(S,s2); for (int i= ;i<n ;i++)
{
char c=str[i-][];
for (int j= ;j<len ;j++)
str[i][j-]=str[i-][j];
str[i][len-]=c;
strcpy(s2,str[i]);
int num=-(s2[]-'');
for (int j= ;j<len ;j++)
{
int k=(s2[j]-''+num)%;
s2[j]=k+'';
}
if (strcmp(S,s2)>) strcpy(S,s2);
}
printf("%s\n",S);
}
return ;
}

后续:感谢提出宝贵的意见。。。