#abc302c. C - Almost Equal

C - Almost Equal

Score : 250250 points

问题描述

给定 NN 个长度为 MM 的字符串 S1,S2,,SNS_1,S_2,\dots,S_N,每个字符串仅包含小写英文字母,并且这些字符串两两不同。

确定是否可以重新排列这些字符串以得到一个新的字符串序列 T1,T2,,TNT_1,T_2,\dots,T_N,满足以下条件:

  • 对于所有整数 ii,其中 1iN11 \le i \le N-1,可以通过将 TiT_i 中恰好一个字符更改为另一个小写英文字母,使其等于 Ti+1T_{i+1}

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given NN strings S1,S2,,SNS_1,S_2,\dots,S_N, each of length MM, consisting of lowercase English letter. Here, SiS_i are pairwise distinct.

Determine if one can rearrange these strings to obtain a new sequence of strings T1,T2,,TNT_1,T_2,\dots,T_N such that:

  • for all integers ii such that 1iN11 \le i \le N-1, one can alter exactly one character of TiT_i to another lowercase English letter to make it equal to Ti+1T_{i+1}.

Constraints

  • 2N82 \le N \le 8
  • 1M51 \le M \le 5
  • SiS_i is a string of length MM consisting of lowercase English letters. (1iN)(1 \le i \le N)
  • SiS_i are pairwise distinct.

Input

The input is given from Standard Input in the following format:

NN MM

S1S_1

S2S_2

\vdots

SNS_N

Output

Print Yes if one can obtain a conforming sequence; print No otherwise.

Sample Input 1

4 4
bbed
abcd
abed
fbed

Sample Output 1

Yes

One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.

Sample Input 2

2 5
abcde
abced

Sample Output 2

No

No matter how the strings are rearranged, the condition is never satisfied.

Sample Input 3

8 4
fast
face
cast
race
fact
rice
nice
case

Sample Output 3

Yes

update @ 2024/3/10 08:29:19