Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing
Posted ydddd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
以上是关于Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 7 A
Educational Codeforces Round 7
Educational Codeforces Round 90
Educational Codeforces Round 33