您好,欢迎来到百家汽车网。
搜索
您的当前位置:首页Erase First or Second Letter

Erase First or Second Letter

来源:百家汽车网

K. Erase First or Second Letter

Problem

You are given a string s s s of length n n n. Let’s define two operations you can apply on the string:

  • remove the first character of the string;
  • remove the second character of the string.

Your task is to find the number of distinct non-empty strings that can be generated by applying the given operations on the initial string any number of times (possibly zero), in any order.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105) — the length of the string.

The second line of each test case contains the string s s s. It is guaranteed that the string only contains lowercase letters of the English alphabet.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer: the number of distinct non-empty strings you can get.

Example

Input
5
5
aaaaa
1
z
5
ababa
14
bcdaaaabcdaaaa
20
abcdefghijklmnopqrst
Output
5
1
9
50
210

Note

In the first test case, we can get the following strings: a a a, a a aa aa, a a a aaa aaa, a a a a aaaa aaaa, a a a a a aaaaa aaaaa.

In the third test case, for example, the word b a ba ba can be reached in the following way:

  • remove the first character of the current string a b a b a ababa ababa, getting b a b a baba baba;
  • remove the second character of the current string b a b a baba baba, getting b b a bba bba;
  • remove the second character of the current string b b a bba bba, getting b a ba ba.

Code

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

void solve()
{
    ll n;
    string s;
    cin>>n>>s;

    map<char,ll> m;
    ll ans=0,sum=0;
    for(ll i=0;i<s.size();i++)
    {
        if(!m[s[i]]) sum++;
        m[s[i]]=1;
        ans+=sum;
    }
    cout<<ans<<endl;
}

int main()
{
    ll t;
    cin>>t;
    while(t--) solve();
    
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baijiahaobaidu.com 版权所有 湘ICP备2023023988号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务