Subject to the effect:
Now has a length of string S n, where each letter is the lowercase letter m before
calculating the number of different length n T (where T m is also written by the first small letters), and the S and T n-1 LCS is
the longest sequence is present in both LCS and S T,
Problem-solving ideas:
The adjacent and the same letters as a block, each block of the answers contribution
We found that when a string is:
, the answer is repeated, that is also equally spaced letters will be repeated, then we can infer that if separated
a repeat
Secondary
Then you can obtain answers
#include<cstdio>
#define ll long long
using namespace std;
const ll N = 100005;
ll n, m, ans, k;
char s[N];
int main() {
scanf("%lld %lld", &n, &m);
scanf("%s", s+1);
ans = 1;
for (ll i = 2; i <= n; ++i)
ans += (s[i] != s[i-1]);
ans *= n * m - n; k = 1;
for (ll i = 2;i <= n; ++i)
if (k == 1) k += (s[i] != s[i-1]);
else if(s[i] == s[i-2]) ++k;
else {
ans -= k * (k-1) / 2;
k = (s[i] != s[i-1]) + 1;
}
ans -= k * (k-1) / 2;
printf("%lld", ans);
}