上进的凡凡,幸运数组,字符串魔法(easy)

上进的凡凡
code1:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;
ll a[N],f[N];
int main()
{
	int t;
	cin >> t;
	for(int i = 1; i <= t; ++i)
	{
		f[i] = 1;
		cin >> a[i];
		if(a[i] >= a[i-1])
			f[i] += f[i-1];
		ans += f[i];
	}
	cout << ans << endl;
	return 0;
}

code2:

#include <iostream>
using namespace std;
int main() {
    int last = 0;
    int temp;
    int n;
    long long res = 0;
    long long count = 0;
    cin >> n;
    while (n--) {
        cin >> temp;
        //如果大于等于上一个数,nice数组长度+1
        //否则计算子数组的个数,长度重置为1
        if (temp >= last) {
            count++;
        } else {
            res += count * (count + 1) / 2;// 连续的count个,等于1+...+count,即等差数列求和公式
            count = 1;
        }
        last = temp;
    }
    //处理一下最后剩下的nice数组
    res += count * (count + 1) / 2;
    cout << res;
}

幸运数组
这道题关键是想出来用前缀和,当 sum[i] % k , 即余数出现过相同的,那么就可以用当前的位置减去第一次出现的位置;当 sum[i] == 0 ,维护一下max就行。
至于为什么余数相同,因为余数相同,相减得到的就是倍数。

#include <iostream>
#include <memory.h>
using namespace std;
int sum[100005];
int pos[100005];
int main() 
{
    int temp;
    int k, t, n;
    cin >> t;
    while (t--) 
	{
        memset(pos, 0, sizeof(pos));
        int Max = -1;
        cin >> n >> k;
        for (int i = 1; i <= n; ++i) 
		{
            cin >> temp;
            sum[i] = (sum[i - 1] + temp) % k;
            if(sum[i] == 0){
            	Max = max(Max, i);continue;
			}
            if (!pos[sum[i]]) 
				pos[sum[i]] = i;
				// pos[i]记录是第一次出现这个余数的位置
				// 因为是从前往后扫,所以存第一个出现的位置可以扫出最大长度 
             else 
                Max = max(Max, (i - pos[sum[i]]));  
        }
        cout << Max << endl;
    }
}

B字符串easy
从题意上来看,串的形式内容就是“AAAABBBBBBBAAAAAABBAAABBBB”

无非就是用一段 B + 一段 AB 或者 一段AB + AB 求最长的一个

#include<bits/stdc++.h>
using namespace std;
#define tb std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int a[200010];
int main()
{
    tb;
    string s;
    int n, k = 0, maxx = 0;
    cin>>n>>s;
    for(int i = 0; i < n; ){
        int cnt = 0;
        while(s[i] == 'A')cnt++, i++;
        while(s[i] == 'B')cnt++, i++;
        a[k++] = cnt;
    }
    for(int i = 0; i < k; i++)
        maxx = max(maxx, a[i] + a[i+1]);
    cout<<maxx;
    return 0;
}

毅神代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int maxn=200000+50;
int n,m,ans,f[maxn],cnt;
char ch[maxn];
int main()
{
    scanf("%d%s",&n,ch+1);
    ch[0]='C';
    for (int i=1; i<=n; ++i)
        if (ch[i]<ch[i-1]) f[++cnt]=1;
        else ++f[cnt];
    for (int i=1; i<=cnt; ++i)
        ans=max(ans,f[i]+f[i-1]);
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值