练习cf1922AA. Tricky Template
题目如下
A. Tricky Template
time limit per test2 seconds
memory limit per test256 megabytes
You are given an integer 𝑛 and three strings 𝑎,𝑏,𝑐, each consisting of 𝑛 lowercase Latin letters.
Let a template be a string 𝑡 consisting of 𝑛 lowercase and/or uppercase Latin letters. The string 𝑠 matches the template 𝑡 if the following conditions hold for all 𝑖 from 1 to 𝑛:
if the 𝑖-th letter of the template is lowercase, then 𝑠𝑖 must be the same as 𝑡𝑖;
if the 𝑖-th letter of the template is uppercase, then 𝑠𝑖 must be different from the lowercase version of 𝑡𝑖. For example, if there is a letter 'A' in the template, you cannot use the letter 'a' in the corresponding position of the string.
Accordingly, the string doesn't match the template if the condition doesn't hold for at least one 𝑖.
Determine whether there exists a template 𝑡 such that the strings 𝑎 and 𝑏 match it, while the string 𝑐 does not.
Input
The first line contains an integer 𝑡 (1≤𝑡≤1000) — the number of test cases.
The first line of each test case contains an integer 𝑛 (1≤𝑛≤20) — the length of the given strings.
The next three lines contain the strings 𝑎,𝑏 and 𝑐. Each string consists of exactly 𝑛 lowercase Latin letters.
Output
For each testcase, print "YES" if there exists a template 𝑡 such that the strings 𝑎 and 𝑏 match it, while the string 𝑐 does not. Otherwise, print "NO".
题目大意
好复杂
大意是现有a,b,c三个由小写字母构成的字符串,是否可以构造一个模版t,使得a和b都能够匹配但是无法与c匹配。
关于模版t的规则如下:
如果t[i]是任意一个大写字母,那么若要与之匹配,字符串a,b中的对应位置a[i],b[i]只要不是对应的小写字母即可匹配上模版,若是对应小写就不能匹配上;
题目分析
根据规则,可以分为两种情况,
第一种,若a和b可以直接匹配上(二者字符串相同),那么模版只要是和a,b一致的小写字符串即可,另外只要满足c与二者不匹配即可;
第二种,若a和b无法直接匹配上(二者字符串不同),那么就尝试用大写的模版来匹配,例如a[i] = a和b[i] = b 不匹配,那么模版取C即可,同时还要满足与c[i]不匹配;
但是实际在代码中没有体现模版以及大小写情况,只考虑a和b的匹配性即可
代码如下
点击查看代码
#include <stdio.h>
#include <string.h>
int main() {
int t;
scanf("%d", &t);
while (t--){
int n;
char a[25], b[25], c[25];
scanf("%d", &n);
scanf("%s", a);
scanf("%s", b);
scanf("%s", c);
int possible = 0;
for(int i = 0; i < n; i++){
if((a[i] == b[i]) && (c[i] != a[i])){
possible = 1;
break;
}
if((a[i] != c[i]) && (b[i] != c[i])){
possible = 1;
break;
}
}
if(possible){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}

浙公网安备 33010602011771号