#P5841. [CTSC2011] 字符串重排
[CTSC2011] 字符串重排
Description
For two strings and , define the length of their longest common prefix 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 pairwise distinct non-empty strings consisting of lowercase letters, for a permutation of to , define the value 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 .
In addition, there are extra tasks. For the -th task, two different integers and between and are given. For a permutation , if under the condition that , it also satisfies that the -th string is placed immediately before the -th string , i.e. , where denotes the position of string in the permutation, then the permutation will additionally receive a reward of . The sum of rewards of all tasks is called the total task reward.
Let a permutation that can maximize the total task reward be .
Find:
- , the maximum value possible.
- , a permutation that, while ensuring the maximum value, can maximize the total task reward.
Input Format
The first line contains two integers and , representing the number of strings and the number of extra tasks, separated by a space.
In the next lines, the strings are given. The -th line contains a string .
In the next lines, the extra tasks are given. The -th line contains two integers and , separated by a space.
Output Format
The output contains three lines.
The first line contains a non-negative integer .
The second line contains several numbers separated by single spaces. The first number is the number of satisfied extra tasks , followed by numbers which are the indices of these tasks. The indices start from and must be output in increasing order.
The third line contains positive integers separated by spaces, representing a permutation of to .
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 points.
- If the second line of the output file is correct, you can get points.
- If the third line of the output file is correct, you can get points.
- If all three lines are correct, you can get 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 of the data, , , and each string length is at most .
- For of the data, , , and each string length is at most .
- For of the data, , and each string length is at most .
- For of the data, no string is a prefix of any other string.
- For of the data, , , each string length is at most , and the sum of all string lengths is at most .
Translated by ChatGPT 5
京公网安备 11011102002149号