[ICPC 2024 Hangzhou R] AUS题解

time limit per test

1 second

memory limit per test

1024 megabytes

Pigeland University will host the 2224 Animal Collegiate Programming Contest (ACPC 2224). Unlike previous years, where teams from the host institution were marked as unofficial, Pigeland will send official teams to compete in the contest with three workstations for each team. Nevertheless, Pig-head, the coach of Pigeland University, remains uncertain about his teams' chances of securing gold medals. As a result, he decides to acquire the contest problems in advance from the AUS problem-setting group, using the excuse of needing to upload data to the online judge.

To prevent cheating, AUS attempts to encrypt the problems using a special cipher. Specifically, problems are represented by strings consisting of lowercase English letters. AUS wants to design a cipher function f(x) that maps lowercase English letters to lowercase English letters. For a problem S=s1s2…sn, the encrypted version of the problem is another string given by F(S)=f(s1)f(s2)…f(sn). For example, when S=abcabc and f(a)=a, f(b)=k, f(c)=a, the encrypted version is F(S)=akaaka.

As a member of AUS, your task is to design the cipher function f. The leader of AUS believes that the function is strong if and only if there exists at least one problem that can be encrypted into the same encrypted version as another, while not all problems produce the same encrypted output. To validate this, he will give you three problems S1, S2, and S3, and you need to find a cipher function f such that F(S1)=F(S2) and F(S1)≠F(S3). Since AUS has several experienced members, your task is simply to determine whether such a cipher function exists.

有道 翻译

Pigeland大学将举办2224大学生动物编程竞赛(ACPC 2224)。与往年不同的是,来自主办机构的团队被标记为非官方,Pigeland将派出官方团队参加比赛,每个团队有三个工作站。尽管如此,猪头,猪兰大学的教练,仍然不确定他的球队获得金牌的机会。因此,他决定以需要向在线评委上传数据为借口,提前从AUS问题设置组获取比赛问题。

为了防止作弊,AUS尝试使用特殊的密码对问题进行加密。具体来说,问题由由小写英文字母组成的字符串表示。AUS想要设计一个密码函数 f(x) ,将小写英文字母映射为小写英文字母。对于问题 S=s1s2…sn ,问题的加密版本是 F(S)=f(s1)f(s2)…f(sn) 给出的另一个字符串。例如: S=abcabc 和 f(a)=a 、 f(b)=k 、 f(c)=a ,则加密版本为 F(S)=akaaka 。

作为AUS的一员,您的任务是设计密码函数 f 。AUS的领导者认为,当且仅当存在至少一个问题可以被加密成与另一个问题相同的加密版本时,该函数是强的,而并非所有问题都产生相同的加密输出。为了验证这一点,他会给你三个问题 S1 , S2 ,和 S3 ,你需要找到一个密码函数 f 这样的 F(S1)=F(S2) 和 F(S1)≠F(S3) 。由于AUS有几个经验丰富的成员,您的任务就是确定这样的密码函数是否存在。

Input

There are multiple test cases. The first line contains an integer T (1≤T≤104) indicating the number of test cases. For each test case:

The first line contains a string S1 (1≤|S1|≤103) consisting of only lowercase English letters.

The second line contains a string S2 (1≤|S2|≤103) consisting of only lowercase English letters.

The third line contains a string S3 (1≤|S3|≤103) consisting of only lowercase English letters.

It is guaranteed that the sum of |S1|+|S2|+|S3| of all test cases does not exceed 3×104.

有道 翻译

输入** **

有多个测试用例。第一行包含一个整数 T ( 1≤T≤104 ),表示测试用例的数量。对于每个测试用例:

第一行包含一个仅由小写英文字母组成的字符串 S1 ( 1≤|S1|≤103 )。

第二行包含一个仅由小写英文字母组成的字符串 S2 ( 1≤|S2|≤103 )。

第三行包含一个仅由小写英文字母组成的字符串 S3 ( 1≤|S3|≤103 )。

保证所有测试用例的 |S1|+|S2|+|S3| 之和不超过 3×104 。

Output

For each test case, if such cipher function exists, output YES in one line. Otherwise, output NO instead.

有道 翻译

** **输出

对于每个测试用例,如果存在这样的密码函数,则一行输出YES。否则,输出NO。

Example

Input

Copy



4

abab

cdcd

abce

abab

cdcd

abcd

abab

cdcd

abc

x

yz

def

Output

Copy

YES
NO
YES
NO

Note

For the first and third sample test cases, one valid cipher function can be f(a)=f(b)=f(c)=f(d)=a and f(e)=b.

有道 翻译

注意

对于第一个和第三个示例测试用例,一个有效的密码函数可以是 f(a)=f(b)=f(c)=f(d)=a 和 f(e)=b 。

思路

用并查集维护即可。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,a1,a2,a3,mod=998244353,f[300005],u,v,lk=0;
string s1,s2,s3;
long long find(long long xx){
	if(f[xx]==xx){
		return xx;
	}
	else{
		f[xx]=find(f[xx]);
		return f[xx];
	}
}
int main(){
	cin>>t;
	while(t--){
		cin>>s1>>s2>>s3;
		if(s1.size()!=s2.size()){
			cout<<"NO"<<endl;
		}
		else if(s1.size()==s2.size()&&s1.size()!=s3.size()){
			cout<<"YES"<<endl;
		}
		else{
			a1=s1.size();
			for(int i=0;i<27;i++){
				f[i]=i;
			}
			for(int i=0;i<a1;i++){
				if(s1[i]!=s2[i]){
					u=find((long long)(s1[i]-'a'));
					v=find((long long)(s2[i]-'a'));
					if(u!=v){
						f[u]=v;
					}
				}
			}
			lk=0;
			for(int i=0;i<a1;i++){
				u=find((long long)(s1[i]-'a'));
				v=find((long long)(s3[i]-'a'));
				if(u!=v){
					lk=1;
					break;
				}
			}
			if(lk==0){
				cout<<"NO"<<endl;
			}
			else{
				cout<<"YES"<<endl;
			}
		}
	}
	return 0;
}

posted @ 2025-10-11 18:18  bz02_2023f2  阅读(6)  评论(0)    收藏  举报  来源