Codeforces 1744A. Number Replacement

本文最后更新于:1 年前

A. Number Replacement
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
An integer array a1,a2,…,an is being transformed into an array of lowercase English letters using the following prodecure:

While there is at least one number in the array:

Choose any number x from the array a, and any letter of the English alphabet y.
Replace all occurrences of number x with the letter y.
For example, if we initially had an array a=[2,3,2,4,1], then we could transform it the following way:

Choose the number 2 and the letter c. After that a=[c,3,c,4,1].
Choose the number 3 and the letter a. After that a=[c,a,c,4,1].
Choose the number 4 and the letter t. After that a=[c,a,c,t,1].
Choose the number 1 and the letter a. After that a=[c,a,c,t,a].
After the transformation all letters are united into a string, in our example we get the string “cacta”.

Having the array a and the string s determine if the string s could be got from the array a after the described transformation?

Input
The first line contains a single integer t (1≤t≤103) — the number of test cases.

Then the description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤50) — the length of the array a and the string s.

The second line of each test case contains exactly n integers: a1,a2,…,an (1≤ai≤50) — the elements of the array a.

The third line of each test case contains a string s of length n, consisting of lowercase English letters.

Output
For each test case, output “YES”, if we can get the string s from the array a, and “NO” otherwise. You can output each letter in any case.

Example
inputCopy
7
5
2 3 2 4 1
cacta
1
50
a
2
11 22
ab
4
1 2 2 1
aaab
5
1 2 3 2 1
aaaaa
6
1 10 2 9 3 8
azzfdb
7
1 2 3 4 1 1 2
abababb
outputCopy
YES
YES
YES
NO
YES
YES
NO
Note
The first test case corresponds to the sample described in the statement.

In the second test case we can choose the number 50 and the letter a.

In the third test case we can choose the number 11 and the letter a, after that a=[a,22]. Then we choose the number 22 and the letter b and get a=[a,b].

In the fifth test case we can change all numbers one by one to the letter a.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include<bits/stdc++.h>
using namespace std;
int main(void) {
int t,n;
string s;
int a[55];
cin >> t;
while (t--) {
int b[55] = { 0 };
int flag = 1;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> s;
for (int i = 0; i < n; i++) {
if (b[a[i]] != 0 && b[a[i]] != s[i]) {
flag = 0;
break;
}
b[a[i]] = s[i];
}
if (flag) { cout << "YES" << endl; }
else {cout << "NO" << endl; }
}
}
//code by yxisme;
//code by zeitspeed;
//welcome to yxisme.com;

Codeforces 1744A. Number Replacement
https://zeitspeed.github.io/2022/11/11/CodeForces1744A. Number Replacement/
作者
yxisme
发布于
2022年11月11日
许可协议