codechef+Name Reduction

本文介绍了一种姓名验证算法,该算法通过排序和比较父母姓名与子女姓名的组合来判断子女姓名是否为父母姓名的一种排列组合子串。具体实现包括输入父母姓名、子女数量及各子女姓名,随后对这些字符串进行排序并逐一验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Name Reduction

Problem code: NAME1

 

In an attempt to reduce the growing population, Archer was asked to come up with a plan. Archer being as intelligent as he is, came up with the following plan:

If N children, with names C1, C2, ..., CN, are born to parents with names A and B, and you consider C to be the concatenation of all the names of the children, i.e. C = C1 + C2 + ... + CN (where + is concatenation operator), then C should be a substring of one of the permutations of A + B.

You are given the task to verify whether the names parents propose to give their children are in fact permissible by Archer's plan or not.

Input

The first line contains an integer T, the number of test cases. T test cases follow. Each test case stats with a line containing two space separated strings A and B, denoting the names of the parents. The next line contains a single integer N denoting the number of children A and B are planning to have. Following this are N lines, the i'th line containing Ci, the proposed name for the i'th child.

Output

For each test case output a single line containing "YES" if the names are permissible by Archer's plan, otherwise print "NO". (quotes are meant for clarity, please don't print them)

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 1000
  • The lengths of all the strings including A, B, and all Ci will be in the range [1, 40000], both inclusive. All these strings will contain only lowercase English letters.
  • The combined lengths of all names of children will not exceed the combined length of the names of their parents.

Example

Input:3tom marvoloriddle2lordvoldemortcheap up1heapcupbruce wayne2batmanOutput:YESYESNO

Explanation:

Let Y denote the concatenation of names of all the children, and X denote the concatenation of the names of the parents.

Case 1: Here X = "tommarvoloriddle", and Y = "lordvoldemort". Consider Z = "iamlordvoldemort". It is not difficult to see that Z is a permutation of X and Y is a substring of Z. Hence Y is a substring of a permutation of X, so the answer is "YES".

Case 2: Here X = "cheapup", and Y = "heapcup". Since Y in itself is a permutation of X, and as every string is a substring of itself, Y is a substring of X and also a permutation of X. Hence "YES".

Case 3: Here X = "brucewayne", and Y = "batman". As "t" is not present in X, "t" wont be present in any permutation of X, hence the answer is "NO".


 

/*
题目大意:给你双亲的姓名,然后给你n个孩纸的姓名,问你双亲合起来的字符串能够、否经过变形包含
孩纸的字符串和;
思路:将双亲和孩纸字符串按字典序排序,然后遍历即可
*/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b;
		a=a+b;
		sort(a.begin(),a.end());
		int i,n;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>b;
			c+=b;
		}
		sort(c.begin(),c.end());
		int k=0;
		for(i=0;i<a.length();i++)
			if(a[i]==c[k])
				k++;
			if(k==c.length())
				cout<<"YES"<<endl;
			else
				cout<<"NO"<<endl;
	//	cout<<a<<" "<<c<<endl;
	}
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值