首页 > 代码库 > CodeForces 451D Count Good Substrings

CodeForces 451D Count Good Substrings

哎,最近都在做图论,没有练DP,现在一遇到DP就不会了= =

因为有合并这个操作,所以只要是首位相同的字符串肯定是能够构成good串的,那么只要统计在奇数位上出现的0,1的个数和偶数位数,随便递推一下就出来了

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 100005;LL cntodd[2],cnteven[2];char buf[maxn];int main() {    scanf("%s",buf + 1);    int len = strlen(buf + 1);    LL sumodd = 0,sumeven = 0;    for(int i = 1;i <= len;i++) {        int now = buf[i] - ‘a‘;        if(i & 1) cntodd[now]++;        else cnteven[now]++;        if(i & 1) {            sumodd += cntodd[now];            sumeven += cnteven[now];        } else {            sumodd += cnteven[now];            sumeven += cntodd[now];        }    }    cout << sumeven << " " << sumodd << endl;    return 0;}