CodeForces 496B Secret Combination

Secret Combination

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 496B

Description

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.

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of digits on the display.

The second line contains n digits — the initial state of the display.

Output

Print a single line containing n digits — the desired state of the display containing the smallest possible number.

Sample Input

Input

3579

Output

024

Input

42014

Output

0142

 1 #include <stdio.h>
 2 #include <string.h>
 3 int main()
 4 {
 5     int n;
 6     int i,j,k;
 7     char a[1005];
 8     int b[1005];
 9     while(scanf("%d",&n)!=EOF)
10     {
11         b[0]=0;
12         for(i=1;i<1000;i++)
13             b[i]=9;
14         scanf("%s",a);
15         for(i=0;i<n;i++)
16         {
17             int x=‘9‘-a[i]+1;
18             for(j=1;j<n;j++)
19             {
20                 int y=(a[(i+j)%n]-‘0‘+x)%10;
21                 if(y<b[j])
22                 {
23                     for(k=1;k<n;k++)
24                     {
25                         b[k]=(a[(i+k)%n]-‘0‘+x)%10;
26                     }
27                     break;
28                 }
29                 else if(y>b[j])
30                 {
31                     break;
32                 }
33             }
34         }
35         for(i=0;i<n;i++)
36             printf("%d",b[i]);
37         printf("\n");
38     }
39     return 0;
40 }

时间: 2025-01-06 14:05:27

CodeForces 496B Secret Combination的相关文章

codeforces 496B. Secret Combination 解题报告

题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个位数)   2.循环右移(最右边那个数字去到第一位上).问经过若个两种操作的组合后,得到的最小数值为多少. 我一开始用了vector来做= =,没有考虑到循环右移的情况.以为每一位从1加到9之后,找出最小的那个就可以..... 留个纪念(错误代码,别学,如果没有循环右移的限制,这个是对的) 1 #incl

cf 496B Secret Combination

题目链接: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 th

构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

题目传送门 1 /* 2 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 8:43:02 7 File Name :A.cpp 8 *************************************************/ 9 1

Codeforces 1263D. Secret Passwords(并查集,思维)

传送门 题意: 给n个字符串,如果任意两个字符串有一个字符相同,则他们相同即属于同一集合,问总共有几个集合 思路: 并查集,对于每个字符串,把每个字符与第一个字符合并,因为他们肯定在同一个集合(意思是每个字符串都当成ch[0],看有几个不同的),合并之后,for(1-26)以它为根的个数,即集合的个数,貌似是思维题(并查集裸题) 代码: #include <iostream> #include <stdio.h> #include <algorithm> #includ

Codeforces Round #301 解题报告

感觉这次的题目顺序很不合理啊... A. Combination Lock Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock. The combination loc

CodeForces Round #283 Div.2

A. Minimum Difficulty 题意: 有一个递增序列a,现在要去掉除了第一项和最后一项元素外的某一项,使新数列中相邻元素之差的最大值最小. 分析: 先求出原序列a中,相邻两项元素之差的最大值m. 枚举去掉ai,则新序列相邻元素差的最大值为max{ m, ai+1 - ai },然后记录最小值即可. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn =

A - Combination Lock

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and

Topcoder SRM 146

Div1 300 RectangularGrid 题意:给定一个长方形,问包含有多少不是正方形的小长方形 题解:枚举小长方形的长宽即可 #line 2 "RectangularGrid.cpp" #include<bits/stdc++.h> using namespace std; typedef long long LL; class RectangularGrid { public: long long countRectangles(int width, int h

Topcoder 144-150(未完待续)

SRM 144 Div2 1100 PowerOutage 题意:给定一个有根树,求遍历完整棵树的最小路程 题解:DFS一下,求出叶子节点中到根节点的最远距离,然后把树的所有边相加,乘以二,减去最远距离就是答案 #line 2 "PowerOutage.cpp" #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=50; struct Edge { int to;