码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 451D Count Good Substrings

时间:2014-07-31 23:34:50      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   ar   div   amp   type   

哎,最近都在做图论,没有练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;
}

  

CodeForces 451D Count Good Substrings,布布扣,bubuko.com

CodeForces 451D Count Good Substrings

标签:blog   os   io   for   ar   div   amp   type   

原文地址:http://www.cnblogs.com/rolight/p/3883646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!