Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing

链接:

https://codeforces.com/contest/1278/problem/A

题意:

Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.

Polycarp decided to store the hash of the password, generated by the following algorithm:

take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p′ (p′ can still be equal to p);
generate two random strings, consisting of lowercase Latin letters, s1 and s2 (any of these strings can be empty);
the resulting hash h=s1+p′+s2, where addition is string concatenation.
For example, let the password p= "abacaba". Then p′ can be equal to "aabcaab". Random strings s1= "zyx" and s2= "kjh". Then h= "zyxaabcaabkjh".

Note that no letters could be deleted or added to p to obtain p′, only the order could be changed.

Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.

Your program should answer t independent test cases.

思路:

暴力枚举

代码:

#include<bits/stdc++.h>
using namespace std;

map<char, int> Mp;
string p, s;

bool Check(int x)
{
    map<char, int> Tmp;
    Tmp = Mp;
    for (int i = 0;i < (int)p.size();i++)
    {
        if (Tmp[s[i+x]] < 0)
            return false;
        Tmp[s[i+x]]--;
    }
    for (auto v: Tmp) if (v.second > 0)
        return false;
    return true;
}


int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        Mp.clear();
        cin >> p >> s;
        for (int i = 0;i < (int)p.size();i++)
            Mp[p[i]]++;
        bool flag = false;
        for (int i = 0;i < (int)s.size();i++) if (Check(i))
        {
            flag = true;
            break;
        }
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/12076341.html