[CF] A. Tricky Template - Educational Codeforces Round 161 (Rated for Div. 2)
Solutions Codeforces 杂题 800 Easy-
Lastmod: 2025-01-19 周日 00:12:20

https://codeforces.com/contest/1922/problem/A

题目大意

给出三个小写字母串 $a, b, c \ (1 \le |a| = |b| = |c| \le 20)$。问是否存在某模式串 $p$ 是 $a, b$ 的匹配但不是 $c$ 的匹配。当一个串的所有位置都是匹配时是匹配,否则不是匹配。匹配规则为如果 $p[i]$ 为小写字母则 $p[i] = t[i]$ 为匹配,如果 $p[i]$ 为大写字母则 $lower(p[i]) \neq t[i]$ 为匹配。($lower(c)$ 表示将 $c$ 转化为对应的小写字母)。

简要题解

显然我们如果想让所有位置对 $a, b, c$ 都匹配,只要每个位置 $i$ 选一个不为 $a[i], b[i], c[i]$ 对应的大写字母的大写字母即可。因为我们要让 $c$ 无法匹配,则我们只要找出某个位置能匹配 $a, b$ 而不能匹配 $c$ 即可。

注意到只要 $a[i] = c[i]$ 或者 $b[i] = c[i]$ 则无论 $t[i]$ 选取什么,$a[i]$ 或 $b[i]$ 与 $c[i]$ 对 $t[i]$ 的匹配性是一样的,则无法完成 $a, b$ 匹配而 $c$ 不匹配。而当 $a[i] \neq c[i]$ 且 $b[i] \neq c[i]$ 时,秩序选 $upper(c[i])$ 即可。

复杂度

$T$:$O(n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n; cin >> n;
  string a, b, c; cin >> a >> b >> c;

  for (int i = 0; i < n; i++) {
    if (a[i] != c[i] && b[i] != c[i]) {
      cout << "YES\n";
      return;
    }
  }
  cout << "NO\n";
}

int main() {
  int t = 1; 
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] E. Nested Segments - Codeforces Round 997 (Div. 2)
Next: [CF] B. Forming Triangles - Educational Codeforces Round 161 (Rated for Div. 2)