CF1748B.Diverse Substrings

普及/提高-

通过率:0%

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 .

输入格式

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

The first line of each test case contains a single integer nn ( 1n1051 \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 .

输出格式

For each test case print one integer — the number of diverse substrings of the given string 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.

首页