zoukankan      html  css  js  c++  java
  • codeforces 1084C The Fair Nut and String (dp)

    题目链接:https://codeforces.com/problemset/problem/1084/C

    思想:对于元素不连续的序列统计方案,可以考虑每个元素的贡献,用之前的答案与当前元素组合来求解

    统计出每组 a 的个数,转换成如下问题:
    给定 n 个数,求 n 个数组合的乘积之和

    做法:

    对于每个数,考虑该数对之前所有数的贡献,之前的数的答案乘上这个数(选这个数和之前的数的答案),之前的数的方案数(不选这个数的答案),
    (该数数量)只选这个数的答案,相加,就是前 i 个数的答案

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<stack>
    #include<queue>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 100010;
    const int mod = 1000000007;
    
    int n;
    int c[maxn],dp[maxn];
    char s[maxn];
    
    ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
    
    int main(){
    	scanf("%s",s);
    	n = strlen(s);
    	
    	int cnt = 1;
    	for(int i=0;i<n;++i){
    		if(s[i] == 'a') ++c[cnt];
    		if(s[i] == 'b'){
    			if(c[cnt]) ++cnt;
    		}
    	}
    	
    	dp[1] = c[1];
    	for(int i=2;i<=cnt;++i){
    		dp[i] = (1ll * dp[i-1] * c[i] + dp[i-1] + c[i]) %mod;
    	}
    	
    	printf("%d
    ",dp[cnt]);
    	
    	return 0;
    }
    
  • 相关阅读:
    键值表
    CRC校验方法
    extern 使用
    编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
    学习积累
    二分查找写法
    生活技术常识
    JAVA实例
    JAVA_Sprint学习(一)
    手把手使用Git?
  • 原文地址:https://www.cnblogs.com/tuchen/p/13823694.html
Copyright © 2011-2022 走看看