当前位置: 代码迷 >> 综合 >> CodeForces - 451D Count Good Substrings (数学规律+组合数学)*
  详细解决方案

CodeForces - 451D Count Good Substrings (数学规律+组合数学)*

热度:97   发布时间:2023-11-15 15:28:15.0

题目链接:http://codeforces.com/problemset/problem/451/D

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
const int  maxn =1e5+5;
const int mod=1e9+7;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
char s[maxn];
/*
题目大意:定义一个好字符串为把重复字符都缩成单个字符后,其串是回文的。
计数这样的串有多少种,分奇偶。找规律啊,如果一个子串其首尾字符都是一样的,
那么可以断定这个子串就是好串。
所以根据组合性质,把a字符计数,b字符计数,
ta*(ta+1)/2+tb*(tb+1)/2就是答案总数。下面计数奇数的个数,
其实不难发现,两个位置奇偶性一样的串,
其就是奇数串,所以把a,b的计数再分类,
就相同的方法得到计数就行了。*/int main()
{scanf("%s",s);int len=strlen(s);ll ta=0,tb=0;ll oa=0,ob=0,ea=0,eb=0;for(int i=0;i<len;i++){if(s[i]=='a'){if(i&1) oa++;else ea++;ta++;}else{if(i&1) ob++;else eb++;tb++;}}ll ans=ta*(ta+1)/2+tb*(tb+1)/2;ll tmp=oa*(oa+1)/2+ob*(ob+1)/2+ea*(ea+1)/2+eb*(eb+1)/2;printf("%lld %lld\n",ans-tmp,tmp);return 0;
}

 

  相关解决方案