Educational Codeforces round 78 A、B

Links: https://codeforces.com/contest/1278


A:Shuffle Hashing


The meaning of problems: a string for p may perform a "hash" operation, in the first p element arranged randomly (probably intact), and then were added two strings (which may be empty) in front of and behind p, composition the new string called h. P and h input string, whether or not the output h p may be derived through the hash operation.

    For example: Enter p = "abacaba", h = "zyxaabcaabkjh", output YES

Analysis: hash operations are divided into two steps: ① ② random permutation p of elements in the front and rear, respectively, p plus two new strings of a string. After so First, if the first operation denoted by p p ', p clearly the number of the elements appearing and p' are equal in. Thus detects whether p another string from a random arrangement may be used wherein a number of detection times counted each array element can occur. Next, description ② p 'is a continuous string of substrings h, note PLEN length p, h is hlen, it is PLEN length, whereby we can traverse h from the beginning until the bit 0 bit hlen-plen starting substring of length plen whether p is from randomly arranged.

Code:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int cnt[30];
int main()
{
    int T;cin >> T;
    while(T--)
    {
        string p,h;cin >> p >> h;
        int plen = p.length(),hlen = h.length();
        int ok = -1;
        for(int i = 0;i <= hlen - plen;++i)
        {
            memset(cnt,0,sizeof(cnt));
            string tmp = h.substr(i,plen);
            for(int j = 0;j < plen;++j)
            {
                ++cnt[tmp[j] - 'a'];
                --cnt[p[j] - 'a'];
            }
            ok = 1;
            for(int i = 0;i < 30;++i)
            {
                if(cnt[i] != 0)
                {
                    ok = 0;
                    break;
                }
            }
            if(ok == 1)
                break;
        }
        if(ok == 1)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}

B:A and B


Meaning of the questions: Given two variables a, b. Each time a selected number therebetween, performs an operation: add this number i (i = i-th execution of the current step), find the minimum number of steps a and b can be made equal.

Analysis: After performing the operation n times, for two numbers a and b, the increase is generally dsum = n (n + 1) / 2. But if you want to apply to the overall increase and a and b are two numbers of a single body, then it will become very complicated, because you can not know the first steps is to increase a or b increases. Therefore, after we On the whole, for a and B, and their originally cursum = a + b, n operations performed becomes cursum + dsum = a '+ b' (In this case a ' == b '), then you can gain an important message: cursum + dsum Since this time a' == b ', then cursum + dsum must be a multiple of 2, otherwise it is not true, and secondly, (cursum + dsum) / 2 it must> = max (a, b), because of the extreme side of the case is very small, has been added to the small side, the last value on the right is equal to the larger, if (cursum + dsum) / 2 is not satisfied> = max (a, b), then the number is already applied at this time, although a multiple of 2, but not enough so that the two halves a and b are equal at this time.

Code:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int T;cin >> T;
    while(T--)
    {
        int a,b;cin >> a >> b;
        if(a == b)
        {
            cout << 0 << endl;
            continue;
        }
        int cursum = a + b,n = 1,nsum = 0;
        for(;n <= max(a,b)*2;++n)
        {
            nsum += n;
            if(((cursum + nsum) & 1 ) == 0 && ((cursum + nsum) / 2) >= max(a,b))
            {
                cout << n << endl;
                break;
            }
        }
    }
}



Guess you like

Origin www.cnblogs.com/HotPants/p/12074778.html