JZOJ 4673. 【NOIP2016提高A组模拟7.20】LCS again


题目:

传送门


题意:

现在有一个长度为 n n n的串 S S S,其中每一个字母都是前 m m m个小写字母
计算有多少个不同的长度为 n n n T T T(其中T也是由前 m m m个小写字母组成),并且 S S S T T T L C S LCS LCS n − 1 n-1 n1
L C S LCS LCS就是同时存在于 S S S T T T的最长子序列


分析:

首先考虑挖一个空再填一个字母。
这样方案数为 n ∗ m − n n*m-n nmn
但是我们考虑 a a b aab aab这样的字符,无论挖掉哪个 a a a都是一样的,所有我们将其根据字母相同分为几个块 a ∣ b b ∣ c c ∣ d ∣ e e a|bb|cc|d|ee abbccdee,对于每个快都可以贡献答案 m ∗ n − n m*n-n mnn
然后我们再看 a b a b abab abab有两种方式可以变成 a a b b aabb aabb,也就是隔着相等也会有重复的。而且 a b a b a b ababab ababab时有 3 3 3种重复。也就是隔着相等的越多重复越多,而且每次多出来的都是和之前的重复,也就是隔着相等有 k k k个就有 C k 2 C_{k}^2 Ck2个是重复的,减去之后就可以得出答案了


代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<ctime>
#include<iomanip>
#include<string>
#include<bitset>
#include<deque>
#include<set>
#define LL long long
using namespace std;
inline LL read(){
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
LL n,m,ans,k;
char s[100005];
int main()
{
	n=read();m=read();
	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;
	cout<<ans;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值