思路:
1、一开始的想的是用map记录下每一个出现过的字符串,但是最后空间会不够,所以放弃
2、我们可以发现在经过删除后得到的字符串(长度为 m,原本字符串长度为 n )中,后n - m - 1位的字符永远相同,所以求不同的字符串数量就是求在这些后面字符串的前面,有多少种不同的字母,然后删减次数从 1,慢慢增加到 n - 1,累加起来的结果就是答案了
eg:abcdeeeefg(n = 10),在经过 4 次删除后,后m(10 - 4 - 1)= 5位字符串("eeefg")永远相同
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
int T;
void solve()
{
int n;
string s;
cin >> n >> s;
map<char, int> p;
long long ans = 0, cnt = 0;
for (int i = 0; i < n; i ++ )
{
char t = s[i];
if (p[t] == 0)
{
p[t] ++ ;
cnt ++ ;
}
ans += cnt;
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> T;
while (T -- )
{
solve();
}
return 0;
}
借鉴了一下大佬的题解
602

被折叠的 条评论
为什么被折叠?



