1012
题意
题解
找规律
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
vector <int> a[28];
ll solve(int id) {
ll res = 0;
ll tmp = a[id][0];
ll sum = a[id][0];
for(int i = 1; i < a[id].size(); i++) {
res = (res + ((a[id][i] - a[id][i - 1]) % mod) * tmp) % mod;
tmp = tmp + 2 * sum % mod + a[id][i] % mod;
sum = (sum + a[id][i]) % mod;
}
return res;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
for(int i = 0; i < 28; i++) a[i].clear();
string str;
cin >> str;
ll res = 0;
int cnt = 1;
for(int i = 0; i < 26; i++) {
a[i].push_back(0);
}
for(auto x : str) {
a[x - 'a'].push_back(cnt);
cnt++;
}
int len = str.length();
for(int i = 0; i < 26; i++) {
a[i].push_back(len + 1);
res = (res + solve(i)) % mod;
}
printf("%lld\n", res);
}
}