• codeforces 1307C Cow and Message (dp)


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

    看起来等差数列这个条件非常难做,但仔细分析后发现,任何长度大于 2 的序列,都可以拆分成长度为 1 或 2的序列
    于是从前向后扫,考虑每个位置的字母对答案的贡献

    1. 对长度为 1 的序列的贡献:该字母出现次数 +1
    2. 对长度为 2 的序列的贡献:26 字母与该字母组合成长度为 2 的序列,贡献就是组合的字母在前面出现的次数
    #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;
    
    int n;
    ll dp1[26],dp2[26][26];
    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);
    	
    	for(int i=0;i<n;++i){
    		int a = s[i] - 'a';
    		for(int j=0;j<26;++j){
    			dp2[j][a] += dp1[j];
    		}
    		++dp1[a]; 
    	}
    	
    	ll ans = 0;
    	for(int i=0;i<26;++i){
    		ans = max(ans,dp1[i]);
    		for(int j=0;j<26;++j){
    			ans = max(ans,dp2[i][j]);
    		}
    	}
    	
    	printf("%lld
    ",ans);
    	
    	return 0;
    }
    
  • 相关阅读:
    cocos3 单击
    cocos3 帧动画
    cocos3 动作和帧动画
    cocos3 场景切换特效
    cocos3 场景切换
    cocos3 error C2440
    c++ 匿名函数
    【leetcode】生成每种字符都是奇数个的字符串
    【leetcode】山羊拉丁文
    【leetcode】字符串的最大公因子
  • 原文地址:https://www.cnblogs.com/tuchen/p/13818621.html
Copyright © 2020-2023  润新知