思路:直接暴力从给出的时间一分钟一分钟加到第二天,判断条件为:如果之前时间有3,4,6,7,9直接排除(倒转后改时间无效),如果倒转后时间大于给出的时间范围也无效(倒转中单个数字变化为2->5 5->2 1,0,8不变,再让全部数字倒置一下),如果有满足条件则输出,没有则输出00:00.
Code:
#include<iostream>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int Max = 2e6 + 5;
const ll Mod = 1e9 + 7;
int lst[10] = { 0,3,4,6,7,9 };
map<int, int> ma;
int main()
{
int t;cin >> t;
ma[0] = 0, ma[1] = 1, ma[2] = 5, ma[8] = 8, ma[5] = 2;
while (t--)
{
int h, m;cin >> h >> m;
string str;
int a, b, c, d;
cin >> str;
a = str[0] - '0', b = str[1] - '0', c = str[3] - '0', d = str[4] - '0';
int aa = a * 10 + b, bb = c * 10 + d;
int f = 0;
for (int i = aa;i <= h-1;i++)
{
for (int j = 0;j <= m - 1;j++)
{
if (i == aa && j == 0)j = bb;
a = i / 10, b = i % 10, c = j / 10, d = j % 10;
int dd = 0;
for (int k = 1;k <= 5;k++)
{
if (a == lst[k] || b == lst[k] || c == lst[k] || d == lst[k])dd = 1;
}
if (dd)continue;
a = ma[a], b = ma[b], c = ma[c], d = ma[d];
if (d * 10 + c <= h - 1 && b * 10 + a <= m - 1)
{
f = 1;cout << ma[a] << ma[b] << ":" << ma[c] << ma[d] << endl;break;
}
}
if (f)break;
}
if (f == 0)cout << "00:00" << endl;
}
}