CF1584F.Strange LCS

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn strings s1,s2,…,sns_1, s_2, \ldots, s_n, each consisting of lowercase and uppercase English letters. In addition, it's guaranteed that each character occurs in each string at most twice. Find the longest common subsequence of these strings.

A string tt is a subsequence of a string ss if tt can be obtained from ss by deletion of several (possibly, zero or all) symbols.

给你 nn 个字符串 s1,s2,…,sns_1, s_2, \ldots, s_n,每个字符串均由小写和大写英文字母组成。此外,保证每个字符在每个字符串中至多出现两次。请找出这些字符串的最长公共子序列。

字符串 tt 是字符串 ss 的子序列,当且仅当 tt 可通过从 ss 中删除若干(可能为零个或全部)字符得到。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤51 \leq t \leq 5) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤102 \leq n \leq 10) — the number of strings.

Each of the next nn lines contains the corresponding string sis_i. Each sis_i is non-empty, consists only of uppercase and lowercase English letters, and no character appears more than twice in each string.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤51 \leq t \leq 5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤102 \leq n \leq 10),表示字符串的数量。

接下来的 nn 行中,每行包含一个对应的字符串 sis_i。每个 sis_i 非空,仅由英文字母(大小写)组成,且每个字符串中任意字符至多出现两次。

输出格式

For each test case print the answer in two lines:

In the first line print the length of the longest common subsequence.

In the second line print the longest common subsequence. If there are multiple such subsequences, print any of them.

对每个测试用例,分两行输出答案:

第一行输出最长公共子序列的长度。

第二行输出最长公共子序列。如果存在多个满足条件的子序列,输出其中任意一个即可。

输入输出样例

  • 输入#1

    4
    2
    ABC
    CBA
    2
    bacab
    defed
    3
    abcde
    aBcDe
    ace
    2
    codeforces
    technocup

    输出#1

    1
    A
    0
    
    3
    ace
    3
    coc

说明/提示

In the first test case, the longest common subsequence is "A". There are no common subsequences of length 22.

In the second test case, sets of characters of strings don't intersect, so any non-empty string can't be a common subsequence.

在第一个测试用例中,最长公共子序列是 "A"。不存在长度为 22 的公共子序列。

在第二个测试用例中,两个字符串的字符集合不相交,因此任何非空字符串都不可能是公共子序列。

输入解题思路,AI测评打分。不知道怎么写?

首页