Thứ Năm, 15 tháng 11, 2012

Problem Lucky Mask

Link: http://codeforces.com/problemset/problem/146/B

B. Lucky Mask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 5,17467 are not.
Petya calls a mask of a positive integer n the number that is obtained after successive writing of all lucky digits of number n from the left to the right. For example, the mask of number 72174994 is number 7744, the mask of 7 is 7, the mask of 9999047 is 47. Obviously, mask of any number is always a lucky number.
Petya has two numbers — an arbitrary integer a and a lucky number b. Help him find the minimum number c (c > a) such that the mask of number c equals b.
Input
The only line contains two integers a and b (1 ≤ a, b ≤ 105). It is guaranteed that number b is lucky.
Output
In the only line print a single number — the number c that is sought by Petya.
Sample test(s)
input
1 7
output
7
input
100 47
output
147


Thuật toán:  Cài đặt như yêu cầu bài toán.

Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<utility>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>


using namespace std;

#define oo 1000000005
#define rep(i,s,e) for(int i = s; i < e; i++)
#define lop(i,s,e) for(int i = s; i != e; i++)
#define FOR(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define SZ(x) ((int)(x).size())
#define X first
#define Y second

typedef pair<int,int> II;
typedef pair<II,int> D;
typedef long long LL;

vector<int> V;
map<int,int> M;
set<int> S;
queue<int> Q;
stack<int> ST;

int mask(int _a)
{
int _res = 0;
for(;_a;_a/=10)
{
int x = _a % 10;
if(x == 4 || x == 7)
{
_res = _res * 10 + x;
}
}
return _res;
}

int main()
{
#define Off  true
if(Off)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
int a,b;
cin >> a >> b;
b = mask(b);
// cout << mask(12407);
lop(c,a+1,-1)
if(mask(c) == b)
{
cout << c;
break;
}
return 0;
}







Không có nhận xét nào:

Đăng nhận xét