#P5841. [CTSC2011] 字符串重排

    ID: 4785 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>贪心2011并查集WC/CTSC/集训队Special Judge字典树,Trie 树

[CTSC2011] 字符串重排

Description

For two strings A=a1a2anA = a_1 a_2 \cdots a_n and B=b1b2bnB = b_1 b_2 \cdots b_n, define the length of their longest common prefix lcp(A,B)\text{lcp}(A,B) as follows:

$$\text{lcp}(A,B) = \max \{k|0 \le k \le n,k \le m,a_1 a_2 \cdots a_k = b_1 b_2 \cdots b_k \}$$

Given nn pairwise distinct non-empty strings S1,S2,,SnS_1,S_2,\cdots , S_n consisting of lowercase letters, for a permutation P=(p1,p2,,pn)P=(p_1,p_2,\cdots,p_n) of 11 to nn, define the value W(P)W(P) as:

$$W(P) = \sum_{i=2}^n (\text{lcp}(S_{p_{i-1}},S_{p_i}))^2$$

Let a permutation that can produce the maximum value be PGP^*_G.

In addition, there are qq extra tasks. For the ii-th task, two different integers XiX_i and YiY_i between 11 and nn are given. For a permutation PP, if under the condition that W(P)=W(PG)W(P) = W(P^*_G), it also satisfies that the XiX_i-th string SXiS_{X_i} is placed immediately before the YiY_i-th string SYiS_{Y_i}, i.e. pos(SXi)+1=pos(SYi)\text{pos}(S_{X_i}) + 1 = \text{pos}(S_{Y_i}), where pos(Si)\text{pos}(S_i) denotes the position of string SiS_i in the permutation, then the permutation PP will additionally receive a reward of 2i2^i. The sum of rewards of all tasks is called the total task reward.

Let a permutation that can maximize the total task reward be PBP^*_B.

Find:

  1. W(PG)W(P^*_G), the maximum value possible.
  2. PBP^*_B, a permutation that, while ensuring the maximum value, can maximize the total task reward.

Input Format

The first line contains two integers nn and qq, representing the number of strings and the number of extra tasks, separated by a space.

In the next nn lines, the strings are given. The ii-th line contains a string SiS_i.

In the next qq lines, the extra tasks are given. The ii-th line contains two integers XiX_i and YiY_i, separated by a space.

Output Format

The output contains three lines.

The first line contains a non-negative integer W(PG)W(P^*_G).

The second line contains several numbers separated by single spaces. The first number is the number of satisfied extra tasks kk, followed by kk numbers which are the indices of these tasks. The indices start from 11 and must be output in increasing order.

The third line contains nn positive integers separated by spaces, representing a permutation PBP^*_B of 11 to nn.

4 6
a
b
abc
bc
1 2
1 3
3 1
4 2
2 4
2 4

2
4 1 3 5 6
3 1 2 4

Hint

Scoring

For each test point:

  • If the first line of the output file is correct, you can get 22 points.
  • If the second line of the output file is correct, you can get 44 points.
  • If the third line of the output file is correct, you can get 44 points.
  • If all three lines are correct, you can get 1010 points.

For the permutation in the third question, if multiple solutions exist, outputting any one of them will be accepted.

If you cannot complete some part, please still output according to the required format to avoid judging failure.

Constraints

  • For 10%10\% of the data, n10n \le 10, q=1q = 1, and each string length is at most 5050.
  • For 20%20\% of the data, n50n \le 50, q=1q = 1, and each string length is at most 5050.
  • For 50%50\% of the data, n,q1000n,q \le 1000, and each string length is at most 10001000.
  • For 70%70\% of the data, no string is a prefix of any other string.
  • For 100%100\% of the data, n4×104n \le 4 \times 10^4, q105q \le 10^5, each string length is at most 10410^4, and the sum of all string lengths is at most 2×1052 \times 10^5.

Translated by ChatGPT 5