ACM修炼之路

**

CF 1278A Shuffle Hashing(暴力)

**

1.题目

题目链接(https://codeforces.com/problemset/problem/1278/A)
A. Shuffle Hashing
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.

The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.

Output
For each test case print the answer to it — “YES” if the given hash h could be obtained from the given password p or “NO” otherwise.

Example
inputCopy
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
outputCopy
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.

In the second test case both s1 and s2 are empty and p′= “threetwoone” is p shuffled.

In the third test case the hash could not be obtained from the password.

In the fourth test case s1= “n”, s2 is empty and p′= “one” is p shuffled (even thought it stayed the same).

In the fifth test case the hash could not be obtained from the password.

2.题意

给出两个串s1,s2。若改变串s1内部顺序后,前后分别加一个串(可以为空串)后等于s2串,则输出YES,否则NO。

3.思路

若s1>s2,肯定输出NO。
若s1<=s2,因为两个串长度小于100,所以先s1排序,然后循环s2找长度为s1的串,每次排序,找到就推出,否则继续找。

4.AC代码


#include"bits/stdc++.h"
using namespace std;
char s1[105],s2[105];
int main()
{
    int t;
    cin>>t;
    getchar();
    while(t--)
    {
        gets(s1);
        gets(s2);
        int l1,l2;
        l1 = strlen(s1);//s1的长度
        l2 = strlen(s2);//s2的长度
        sort(s1,s1+l1);//给s1排序
        if(l1>l2) //如果s1>s2,输出NO
        {
            puts("NO");
            continue;
        }
        bool f;//标记是否找到
        for(int i = 0; i < l2-l1+1 ; i++)
        {
            f = 1;//每次循环都要更新一下
            char t[105];//临时串,拷贝s2,因为每次都挺要排序,会影响串s2的原始顺序
            strcpy(t,s2);
            sort(t+i,t+i+l1);//在s2中给s1长度的子串排序
            for(int j = 0; j < l1; j++)
            {
                if(s1[j]!=t[i+j])
                {
                    f = 0;
                    break;
                }
            }
            if(f)
                break;
        }
        if(f) puts("YES");
        else puts("NO");
    }
    return 0;
    }

你可能感兴趣的:(ACM修炼之路)