JZOJ 4673. [increase] A set of simulated race T3 LCS again

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 n × m n\times m
We found that when a string is: a b a b ABAB , the answer is repeated, that is also equally spaced letters will be repeated, then we can infer that if separated x x a repeat C x 2 C^2_x Secondary

Then you can obtain answers

A c c e p t e d   c o d e : Accepted\ code:

#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);
}

Guess you like

Origin blog.csdn.net/qq_39798042/article/details/88900925