CF1535F.String Distance

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Suppose you are given two strings aa and bb. You can apply the following operation any number of times: choose any contiguous substring of aa or bb, and sort the characters in it in non-descending order. Let f(a,b)f(a, b) the minimum number of operations you have to apply in order to make them equal (or f(a,b)=1337f(a, b) = 1337 if it is impossible to make aa and bb equal using these operations).

For example:

  • f(ab,ab)=0f(\text{ab}, \text{ab}) = 0;
  • f(ba,ab)=1f(\text{ba}, \text{ab}) = 1 (in one operation, we can sort the whole first string);
  • f(ebcda,ecdba)=1f(\text{ebcda}, \text{ecdba}) = 1 (in one operation, we can sort the substring of the second string starting from the 22-nd character and ending with the 44-th character);
  • f(a,b)=1337f(\text{a}, \text{b}) = 1337.

You are given nn strings s1,s2,…,sks_1, s_2, \dots, s_k having equal length. Calculate ∑i=1n∑j=i+1nf(si,sj)\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j).

假设你有两个字符串 aa 和 bb。你可以执行以下操作任意多次:任选 aa 或 bb 的一个连续子串,并将该子串中的字符按非降序排列。定义 f(a,b)f(a, b) 为使 aa 和 bb 相等所需的最少操作次数(若无法通过这些操作使 aa 和 bb 相等,则令 f(a,b)=1337f(a, b) = 1337)。

例如:

  • f(ab,ab)=0f(\text{ab}, \text{ab}) = 0;
  • f(ba,ab)=1f(\text{ba}, \text{ab}) = 1(一次操作即可对第一个字符串整体排序);
  • f(ebcda,ecdba)=1f(\text{ebcda}, \text{ecdba}) = 1(一次操作即可对第二个字符串中从第 22 个字符到第 44 个字符的子串排序);
  • f(a,b)=1337f(\text{a}, \text{b}) = 1337。

给定 nn 个长度相等的字符串 s1,s2,…,sns_1, s_2, \dots, s_n,请计算 ∑i=1n∑j=i+1nf(si,sj)\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j)。

输入格式

The first line contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of strings.

Then nn lines follow, each line contains one of the strings sis_i, consisting of lowercase Latin letters. ∣s1∣=∣s2∣=…=∣sn∣|s_1| = |s_2| = \ldots = |s_n|, and n⋅∣s1∣≤2⋅105n \cdot |s_1| \le 2 \cdot 10^5. All these strings are pairwise distinct.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 字符串的数量。

接下来有 nn 行,每行包含一个字符串 sis_i,由小写拉丁字母组成。满足 ∣s1∣=∣s2∣=…=∣sn∣|s_1| = |s_2| = \ldots = |s_n|,且 n⋅∣s1∣≤2⋅105n \cdot |s_1| \le 2 \cdot 10^5。所有这些字符串两两互不相同。

输出格式

Print one integer: ∑i=1n∑j=i+1nf(si,sj)\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j).

输出一个整数:∑i=1n∑j=i+1nf(si,sj)\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j)。

输入输出样例

  • 输入#1

    4
    zzz
    bac
    abc
    acb

    输出#1

    4015
  • 输入#2

    2
    a
    b

    输出#2

    1337

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

首页