Codeforces 451D - Count Good Substrings(组合数学)

来源:互联网 发布:手机ai软件下载 编辑:程序博客网 时间:2024/05/21 17:28
/*题目链接:http://codeforces.com/problemset/problem/451/D题目大意:定义good string,就是就一个字符串的连续相同字符用一个该字符替代后,    形成回文串的字符串。现在给出一个字符串,问说该字符串的子串中,    为good string的串有多少个,分长度为奇数和偶数的输出。因为字符串的组成为a和b,所以只要是头尾相同的子串都是满足的!!!!    所以我们计算在奇数和偶数位置的奇数个数和偶数个数即可,    然后用组合数学求出答案。*/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <sstream>#include <map>#include <set>#define pi acos(-1.0)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1using namespace std;typedef pair<int, int> P;const double eps = 1e-10;const int maxn = 1e6 + 5;const int N = 1e4 + 5;const int mod = 1e8;char str[maxn];LL fun(LL a){    return a * (a-1) / 2;}int main(void){//freopen("in.txt","r", stdin);    scanf("%s", str+1);    LL evenA = 0, oddA = 0;    LL evenB = 0, oddB = 0;    int len = strlen(str+1);    for (int i = 1; i <= len; i++){        if (str[i] == 'a'){            if (i & 1) oddA++;            else evenA++;        }        else {            if (i & 1) oddB++;            else evenB++;        }    }    LL even = oddA * evenA + oddB * evenB;    LL odd = len + fun(oddA) + fun(evenA) + fun(oddB) + fun(evenB);    cout << even << " " << odd << endl;return 0;}


0 0
原创粉丝点击