CF1748B.Diverse Substrings

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.

For example:

  • string "7" is diverse because 7 appears in it 11 time and the number of distinct characters in it is 11;
  • string "77" is not diverse because 7 appears in it 22 times and the number of distinct characters in it is 11;
  • string "1010" is diverse because both 0 and 1 appear in it 22 times and the number of distinct characters in it is 22;
  • string "6668" is not diverse because 6 appears in it 33 times and the number of distinct characters in it is 22.

You are given a string ss of length nn, consisting of only digits 00 to 99. Find how many of its n(n+1)2\frac{n(n+1)}{2} substrings are diverse.

A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Note that if the same diverse string appears in ss multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in "77" both equal to "7", so the answer for the string "77" is 22.

一个非空的数字字符串被称为“多样化的”,当且仅当其中每个字符的出现次数均不超过该字符串中不同字符的个数。

例如:

  • 字符串 "7" 是多样化的,因为字符 7 在其中出现了 11 次,而该字符串中不同字符的个数为 11;
  • 字符串 "77" 不是多样化的,因为字符 7 在其中出现了 22 次,而该字符串中不同字符的个数为 11;
  • 字符串 "1010" 是多样化的,因为字符 0 和 1 在其中均出现了 22 次,而该字符串中不同字符的个数为 22;
  • 字符串 "6668" 不是多样化的,因为字符 6 在其中出现了 33 次,而该字符串中不同字符的个数为 22。

给定一个长度为 nn 的字符串 ss,它仅由数字 0 到 9 组成。请计算其 n(n+1)2\frac{n(n+1)}{2} 个子串中有多少个是多样化的。

若字符串 aa 可通过从字符串 bb 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到,则称 aa 是 bb 的一个子串。

注意:若同一个多样化的字符串在 ss 中多次出现,则每次出现都应被独立计数。例如,在字符串 "77" 中有两个多样化的子串,均为 "7",因此字符串 "77" 的答案为 22。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the string ss.

The second line of each test case contains a string ss of length nn. It is guaranteed that all characters of ss are digits from 00 to 99.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss。保证 ss 中的所有字符均为数字,取值范围为 00 到 99。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case print one integer — the number of diverse substrings of the given string ss.

对于每个测试用例,输出一个整数——字符串 ss 的不同子串的数量。

输入输出样例

  • 输入#1

    7
    1
    7
    2
    77
    4
    1010
    5
    01100
    6
    399996
    5
    23456
    18
    789987887987998798

    输出#1

    1
    2
    10
    12
    10
    15
    106

说明/提示

In the first test case, the diverse substring is "7".

In the second test case, the only diverse substring is "7", which appears twice, so the answer is 22.

In the third test case, the diverse substrings are "0" (22 times), "01", "010", "1" (22 times), "10" (22 times), "101" and "1010".

In the fourth test case, the diverse substrings are "0" (33 times), "01", "011", "0110", "1" (22 times), "10", "100", "110" and "1100".

In the fifth test case, the diverse substrings are "3", "39", "399", "6", "9" (44 times), "96" and "996".

In the sixth test case, all 1515 non-empty substrings of "23456" are diverse.

在第一个测试用例中,多样子串为 "7"。

在第二个测试用例中,唯一的多样子串是 "7",它出现了两次,因此答案为 22。

在第三个测试用例中,多样子串包括 "0"(出现 22 次)、"01"、"010"、"1"(出现 22 次)、"10"(出现 22 次)、"101" 和 "1010"。

在第四个测试用例中,多样子串包括 "0"(出现 33 次)、"01"、"011"、"0110"、"1"(出现 22 次)、"10"、"100"、"110" 和 "1100"。

在第五个测试用例中,多样子串包括 "3"、"39"、"399"、"6"、"9"(出现 44 次)、"96" 和 "996"。

在第六个测试用例中,字符串 "23456" 的全部 1515 个非空子串均为多样子串。

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

首页