1 second
256 megabytes
standard input
standard output
Do you remember how Kai constructed the word "eternity" using pieces of ice as components?
Little Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number t. He realized that
digits 6 and 9 are very similar, so he can rotate piece of ice with 6 to use as 9 (and vice versa). Similary, 2 and 5 work the same. There is no other pair of digits with similar effect. He called this effect "Digital Mimicry".
Sheldon favourite number is t. He wants to have as many instances of t as
possible. How many instances he can construct using the given sequence of ice pieces. He can use any piece at most once.
The first line contains integer t (1 ≤ t ≤ 10000).
The second line contains the sequence of digits on the pieces. The length of line is equal to the number of pieces and between 1 and 200, inclusive. It contains digits between 0 and 9.
Print the required number of instances.
42
23454
2
169
12118999
1
题意:给一个数t,然后给一个串s,然后找s串中最多出现多少次t。 当中对于数字2与5,6与9能够互相替代。有一个办法是把t和s中5都变成2,9都变成6.
官方标签是贪心。 。 没看出来。感觉还是读题意然后实现
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
char s[1000],c[8];
int num[10];
bool is_ok()
{
for(int i=0;i<strlen(c);i++)
{
if(num[c[i]-'0']<1)
return 0;
num[c[i]-'0']--;
}
return 1;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
memset(num,0,sizeof(num));
scanf("%s",s);
sprintf(c,"%d",t);
for(int i=0;i<strlen(c);i++)
if(c[i]=='9')
c[i]='6';
else if(c[i]=='5')
c[i]='2';
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='9')
s[i]='6';
if(s[i]=='5')
s[i]='2';
num[s[i]-'0']++;
}
int ans=0;
//for(int i=0;i<strlen(c);i++)cout<<num[c[i]-'0']<<" ";cout<<endl;
while(1)
{
if(is_ok())
ans++;
else
break;
}
printf("%d\n",ans);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。