Codeforces Round #104 (Div. 2) A~D

A. Lucky Ticket
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 517467 are not.

Petya loves tickets very much. As we know, each ticket has a number that is a positive integer. Its length equals n (n is always even). Petya calls a ticket lucky if the ticket's number is a lucky number and the sum of digits in the first half (the sum of the first n / 2 digits) equals the sum of digits in the second half (the sum of the last n / 2 digits). Check if the given ticket is lucky.

Input

The first line contains an even integer n (2 ≤ n ≤ 50) — the length of the ticket number that needs to be checked. The second line contains an integer whose length equals exactly n — the ticket number. The number may contain leading zeros.

Output

On the first line print "YES" if the given ticket number is lucky. Otherwise, print "NO" (without the quotes).

Examples
input
Copy
2
47
output
NO
input
Copy
4
4738
output
NO
input
Copy
4
4774
output
YES
Note

In the first sample the sum of digits in the first half does not equal the sum of digits in the second half (4 ≠ 7).

In the second sample the ticket number is not the lucky number.

题意:输入一个字符串长度,再输入这个字符串。判断这个字符串是否全由4和7构成,且字符串前半段和后半段数字之和相等。满足条件,输出“YES",不满足输出"NO"

思路:纯暴力……

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;
    while(cin>>n)
    {
        string s;
        cin>>s;
        int sum1=0,sum2=0;
        bool flag=true;
        for(int i=0;i<n/2;i++)
        {
            if(s[i]!='4'&&s[i]!='7')
            {
                flag=false;
                break;
            }
            sum1+=s[i]-'0';
        }
        for(int i=n/2;i<n;i++)
        {
            if(s[i]!='4'&&s[i]!='7')
            {
                flag=false;
                break;
            }
            sum2+=s[i]-'0';
        }
        if(flag&&sum1==sum2)
        {
            cout<<"YES"<<endl;
        }
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
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 517467 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.

Examples
input
Copy
1 7
output
7
input
Copy
100 47
output
147
题意:输入两个数a和b,定义"a mask of number"为将某个数中不为4和7的数字剔除掉所得到的数字,比如14273447的"a mask of number"就是47447。求一个数c,使c的"a mask of number"等于b且c>a,使c尽可能小。
思路:直接从a往上累加,再暴力一发……

#include <bits/stdc++.h>
using namespace std;
int fun(int n)
{
    vector<int>v;
    while(n)
    {
        int temp=n%10;
        if(temp==4||temp==7)
        {
            v.push_back(temp);
        }
        n/=10;
    }
    int ans=0;
    for(int i=v.size()-1;i>=0;i--)
    {
        ans=ans*10+v[i];
    }
    return ans;
}
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        while(1)
        {
             a++;
            if(fun(a)==b)
                break;
        }
        cout<<a<<endl;
    }
    return 0;
}
C. Lucky Conversion
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 517467 are not.

Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:

  • replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
  • swap any pair of digits in string a.

Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.

Input

The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.

Output

Print on the single line the single number — the minimum number of operations needed to convert string a into string b.

Examples
input
Copy
47
74
output
1
input
Copy
774
744
output
1
input
Copy
777
444
output
3
Note

In the first sample it is enough simply to swap the first and the second digit.

In the second sample we should replace the second digit with its opposite.

In the third number we should replace all three digits with their opposites. 

题意:给出两个只含7和4的字符串a和b,有两种操作,第一种将a中的一个字符变成另一种(7变成4,4变成7),第二种任意交换a中两个字符的位置。问至少经过多少次操作可以把a变成b。

思路:多写几组数据,可以发现,设置两个计数器,分别统计a中为4,b中为7b中为7,a中为4的字符个数,输出最大的那个值

感觉自己说的不是很清楚 直接上代码

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string  a,b;
    while(cin>>a>>b)
    {
        int cnt1=0,cnt2=0;
        for(int i=0;i<a.length();i++)
        {
 
            if(a[i]=='4'&&b[i]=='7')
                cnt1++;
            if(a[i]=='7'&&b[i]=='4')
                cnt2++;
        }
        cout<<max(cnt1,cnt2)<<endl;
    }
    return 0;
}
D. Lucky Number 2
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 517467 are not.

Petya loves long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 747747, then cnt(4) = 2cnt(7) = 4cnt(47) = 2cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1cnt(7) = a2cnt(47) = a3cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.

Input

The single line contains four integers a1a2a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).

Output

On the single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(4) = a1cnt(7) = a2cnt(47) = a3cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).

Examples
input
Copy
2 2 1 1
output
4774
input
Copy
4 7 3 1
output
-1
题意:输入一个字符串中"4"、"7"、"47"和"74"的数目,构造出这个只含4和7的字符串,不能构造输出-1.
思路:想了很长时间,不知道该用啥算法,百度一下?满屏的找规律……求内心阴影面积……

上图是规律

附上自己根据规律的代码:

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int cnt4,cnt7,cnt47,cnt74;
    while(cin>>cnt4>>cnt7>>cnt47>>cnt74)
    {
        if(abs(cnt47-cnt74)>1||cnt4+cnt7<=cnt47+cnt74||cnt4<cnt47||cnt7<cnt47||cnt4<cnt74||cnt7<cnt74)
        {
            cout<<-1<<endl;
            continue;
        }
        else if(cnt47>cnt74)
        {
            cnt4-=cnt74;
            cnt7-=cnt74;
            for(int i=0; i<cnt4; i++)
            {
                cout<<4;
            }
            for(int i=0; i<cnt74; i++)
            {
                cout<<74;
            }
            for(int i=0; i<cnt7; i++)
            {
                cout<<7;
            }
            cout<<endl;
        }
        else if(cnt47==cnt74)
        {
            if(cnt4>cnt47)
            {
                cnt4-=1+cnt47;
                cnt7-=cnt47;
                for(int i=0; i<cnt4; i++)
                {
                    cout<<4;
                }
                for(int i=0; i<cnt47; i++)
                {
                    cout<<47;
                }
                for(int i=0; i<cnt7; i++)
                {
                    cout<<7;
                }
                cout<<4<<endl;
            }
            else
            {
                cnt7-=1+cnt47;
                cout<<7;
                for(int i=0; i<cnt47; i++)
                {
                    cout<<47;
                }
                for(int i=0; i<cnt7; i++)
                {
                    cout<<7;
                }
                cout<<endl;
            }
        }
        else
        {
            cnt4-=cnt47+1;
            cnt7-=cnt47+1;
            cout<<7;
            for(int i=0; i<cnt4; i++)
                {
                    cout<<4;
                }
                for(int i=0; i<cnt47; i++)
                {
                    cout<<47;
                }
                for(int i=0; i<cnt7; i++)
                {
                    cout<<7;
                }
                cout<<4<<endl;
        }
    }
    return 0;
} 

就以上四题啦!E题有知识点还没学……不会写……还得加油啊

猜你喜欢

转载自blog.csdn.net/eknight123/article/details/79859370