B. Planet Lapituletti Codeforces Round #705 (Div. 2)

B. Planet Lapituletti

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts hh hours and each hour lasts mm minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 00 to h−1h−1 and minutes are numbered from 00 to m−1m−1.

That's how the digits are displayed on the clock. Please note that digit 11 is placed in the middle of its position.

A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).

The image of the clocks in the mirror is reflected against a vertical axis.

The reflection is not a valid time.

The reflection is a valid time with h=24h=24, m=60m=60. However, for example, if h=10h=10, m=60m=60, then the reflection is not a valid time.

An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment ss and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.

It can be shown that with any hh, mm, ss such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.

You are asked to solve the problem for several test cases.

Input

The first line contains a single integer TT (1≤T≤1001≤T≤100) — the number of test cases.

The next 2⋅T2⋅T lines contain the description of test cases. The description of each test case consists of two lines.

The first line of a test case contains two integers hh, mm (1≤h,m≤1001≤h,m≤100).

The second line contains the start time ss in the described format HH:MM.

Output

For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.

Example

input

Copy

5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04

output

Copy

12:21
00:00
52:28
00:00
00:00

Note

In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.

 

题目大意:找出最近闹钟的时间从镜子看也是合法。

解题思路:闹钟时间是不断增加的,能够从镜子看到也是合法的数字有0, 1, 2, 5, 8。(1可以是因为题目说1是中间的),然后从镜子看到的时间也要判断合不合法。注意,0从镜子看到的是0、

1 -> 1, 2 -> 5, 5 -> 2, 8 -> 8。

AC代码:

#include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;
 
#define ll long long
#define INF 0x3f3f3f3f;
 
map<int, int>mp;
 
const int MAX = 5;
bool check(int a, int b, int c, int d) {
    if (mp[a] && mp[b] && mp[c] && mp[d]){ 
        return true;
    }
    return false;
}
int main() {
    //map<int, int>m;
    mp[1] = 1;
    mp[2] = 5;
    mp[0] = 10;
    mp[5] = 2;
    mp[8] = 8;
    map<int, int>ans;
    ans[1] = 1;
    ans[2] = 5;
    ans[0] = 0;
    ans[5] = 2;
    ans[8] = 8;
    int t;
    scanf("%d", &t);
    while (t--) {
        int x, y;
        scanf("%d %d", &x, &y);
        int n, m;
        scanf("%d:%d",&n, &m);
        bool f = true;
        while (f) {
            if (m >= y) {
                m = 0;
                n++;
            }
            if (n >= x) {
                n = 0;
            }
            int a,b,c,d;
            a = n / 10;
            b = n % 10;
            c = m / 10;
            d = m % 10;
            if(check(a, b, c, d) && ((ans[d] * 10 + ans[c]) <x) && ((ans[b] * 10 + ans[a]) < y)) {
                f = false;
                printf("%d%d:%d%d\n", a, b, c, d);
            }else{
                m++;
            }
        }
    }
 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值