A. Shuffle Hashing-------思维(水)

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.

题意:给你一个p序列,p’为p序列打乱的,h=s1+p’+s2。这里的+都是连接的意思。现在题目问你给你p和h,检查h是否可以得到p。

解析:这道题只要暴力就行了,因为数据范围很小。分三种情况
序列p的长度为n,序列h的长度为m
第一种m<n 肯定输出NO
第二种m==n 检查自身是不是。
第三种 n>m 暴力遍历就行了

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1000;
int t;
char s[N],str[N];
map<char,int> v,res;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>(s+1)>>(str+1);
		v.clear();res.clear();
		int n=strlen(s+1);
		int m=strlen(str+1);
		for(int i=1;i<=n;i++) v[s[i]]++;
		if(m<n)
		{
			puts("NO");
			continue;
		}
		if(m==n)
		{
			for(int i=1;i<=m;i++) res[str[i]]++;
			int f=1;
			for(int i=1;i<=m;i++)
			{
				if(v[s[i]]!=res[s[i]])
				{
					f=0;
					break;
				}
			 } 
			if(f==1) 
				puts("YES");
			else puts("NO");
			continue;
		}
		int f=1;
		for(int i=1;i<=m-n+1;i++)
		{
			res.clear();
			for(int j=i;j<=i+n-1;j++) res[str[j]]++;
			 f=1;
			for(int j=1;j<=n;j++)
			{
				if(v[s[j]]!=res[s[j]])
				{
					f=0;
					break;
				}
			}
			if(f==1) 
			{
				puts("YES");
				break;
			}
			    
		}
		if(f==0) puts("NO");
	}
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值